Pagini recente » Cod sursa (job #149223) | Cod sursa (job #149221) | Cod sursa (job #149176) | Cod sursa (job #3320896) | Cod sursa (job #3322346)
#include <bits/stdc++.h>
using namespace std;
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");
const int INF = 1e9;
int main()
{
int n;
f >> n;
cout<<n;
vector<vector<int>> dist(n + 1, vector<int>(n + 1, INF));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
int x;
f >> x;
if (i == j) dist[i][j] = 0;
else if (x != 0) dist[i][j] = x;
else dist[i][j]=INF;
}
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (dist[i][k] < INF && dist[k][j] < INF)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if (dist[i][j]== INF) g << 0;
else g << dist[i][j] << ' ';
g << '\n';
}
return 0;
}