Pagini recente » Cod sursa (job #1431345) | Cod sursa (job #2287001) | Cod sursa (job #954504) | Cod sursa (job #1645609) | Cod sursa (job #3130826)
#include <fstream>
using namespace std;
const int N = 100, INF = 1000001;
int cost[N + 1][N + 1];
int main()
{
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
int n;
in >> n;
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= n; j ++)
{
in >> cost[i][j];
if (cost[i][j] == 0 && i != j)
{
cost[i][j] = INF;
}
}
}
for (int k = 1; k <= n; k ++)
{
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= n; j ++)
{
cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
}
}
}
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= n; j ++)
{
if (cost[i][j] == INF)
{
out << 0 << ' ';
}
else
{
out << cost[i][j] << ' ';
}
}
out << '\n';
}
in.close();
out.close();
return 0;
}