Cod sursa(job #698377)

Utilizator soriynSorin Rita soriyn Data 29 februarie 2012 13:42:58
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include<fstream>
#define maxn 105

using namespace std;
int cost[maxn][maxn];
int n;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
void read()
{
	in>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			in>>cost[i][j];
}

void solve()
{
	int k,i,j;
	for(k=1;k<=n;k++)
	   for(i=1;i<=n;i++)
		   for(j=1;j<=n;j++)
		     if( (cost[i][k]!=0) && (cost[k][j]!=0) &&(cost[i][k]+cost[k][j]<cost[i][j] || cost[i][j]==0) && i!=j)
				 cost[i][j]=cost[i][k]+cost[k][j];
}

void write()
{
	for(int i=1;i<=n;i++)
    {
		for(int j=1;j<=n;j++)
			out<<cost[i][j]<<" ";
		out<<"\n";
	}
}

int main()
{
	read();
	solve();
	write();
}