Pagini recente » Cod sursa (job #184364) | Cod sursa (job #642854) | Cod sursa (job #990798) | Cod sursa (job #2261377) | Cod sursa (job #2239566)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
const int INF = 1 << 20;
int D[101][101];
int n, m;
void Roy_Floyd() {
for(int k = 1; k <= n; k++)
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) {
int cost = D[i][k] + D[k][j];
if(D[i][j] > cost)
D[i][j] = cost;
}
}
int main()
{
int x;
in >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) {
in >> x;
if(x == 0 && i != j)
D[i][j] = INF;
else D[i][j] = x;
}
Roy_Floyd();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++)
if(D[i][j] == INF) out << "0 ";
else out << D[i][j] << ' ';
out << '\n';
}
return 0;
}