Cod sursa(job #701582)

Utilizator soriynSorin Rita soriyn Data 1 martie 2012 16:39:01
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include<fstream>
#define maxn 105

using namespace std;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
int cost[maxn][maxn];
int n;

void read()
{
	in>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			in>>cost[i][j];
}

void royfloyd()
{
	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][j]>cost[i][k]+cost[k][j] || cost[i][j]==0) && i!=j)
					cost[i][j]=cost[i][k]+cost[k][j];
}

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