Pagini recente » Cod sursa (job #3328823) | Cod sursa (job #3338527) | Cod sursa (job #3328566) | Cod sursa (job #3351964) | Cod sursa (job #3336402)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
int main()
{
int n;
fin >> n;
int inf = 1e9+1;
vector<vector<int>>ma(n+1, vector<int>(n+1));
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++)
{
fin >> ma[i][j];
if(ma[i][j] != 0)
dist[i][j] = ma[i][j];
if(i == j)
dist[i][j] = 0;
}
}
for(int k = 1; k <= n; k++)
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(dist[i][j] > dist[i][k] + dist[k][j])
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)
fout << 0 << " ";
else
fout << dist[i][j] << " ";
}
fout << "\n";
}
return 0;
}