Pagini recente » Cod sursa (job #2675940) | Cod sursa (job #2361181) | Cod sursa (job #2334758) | Cod sursa (job #680890) | Cod sursa (job #1845620)
//roy-floyd
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
int dist[1000][1000];
void roy_warshall(int n){
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i!=j && dist[i][k] && dist[k][j] && ( dist[i][j]>dist[i][k]+dist[k][j]||!dist[i][j]))
dist[i][j]=dist[i][k]+dist[k][j];
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
fout<<dist[i][j]<<' ';
}
fout<<'\n';
}
}
int main()
{
int n;
fin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
fin>>dist[i][j];
}
roy_warshall(n);
return 0;
}