Pagini recente » Cod sursa (job #2124238) | Cod sursa (job #1971731) | Cod sursa (job #917549) | Cod sursa (job #1336990) | Cod sursa (job #3130850)
#include <fstream>
using namespace std;
const int N = 100;
const int INF = 1e9;
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 (i!= j && cost[i][j] == 0)
{
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 (i != j && cost[i][j] == INF)
{
cost[i][j] = 0;
}
out << cost[i][j] << " ";
}
out << "\n";
}
in.close();
out.close();
return 0;
}