Pagini recente » Cod sursa (job #2679555) | Monitorul de evaluare | Cod sursa (job #915099) | Cod sursa (job #2447826) | Cod sursa (job #1456529)
#include <fstream>
#include <climits>
using namespace std;
int main(int argc, char **argv)
{
ifstream indata("royfloyd.in");
// input data
int n;
indata >> n;
int cost_mat[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
indata >> cost_mat[i][j];
if (cost_mat[i][j] == 0) {
cost_mat[i][j] = INT_MAX;
}
}
}
// apply Roy-Floyd to find shortestPath
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(cost_mat[i][j] > cost_mat[i][k] + cost_mat[k][j])
cost_mat[i][j] = cost_mat[i][k] + cost_mat[k][j];
}
}
}
// output data
ofstream outdata("royfloyd.out");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
outdata << cost_mat[i][j] << " ";
}
outdata << "\n";
}
indata.close();
outdata.close();
return 0;
}