Pagini recente » Cod sursa (job #1205551) | Cod sursa (job #2857088) | Cod sursa (job #2628275) | Cod sursa (job #1985507) | Cod sursa (job #1976191)
#include <iostream>
#include <fstream>
#include<time.h>
#define inf 1001
using namespace std;
int main(){
int dist[101][101],n;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
in>>n;
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
in>>dist[i][j];
if(dist[i][j]==0 && i!=j)//nu avem dist de la i la j
dist[i][j]=inf;
}
}
clock_t t;
t = clock();
for(int k1=1;k1<=1;++k1){
for(int k=1;k<=n;++k){//luam k drept nod intermediar
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][k]!=inf && dist[k][j]!=inf && i!=k && j!=k)//daca dist de la i la j prin k este mai mica
dist[i][j]=dist[i][k]+dist[k][j];//actualizam
}
}
t = clock() - t;
cout<<" execution time: "<<((double)t)/CLOCKS_PER_SEC<<endl;
//cout<<" execution time: "<<(t2-t1);
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
if(dist[i][j]==inf)
out<<"0 ";
else
out<<dist[i][j]<<" ";
}
out<<"\n";
}
return 0;
}