Cod sursa(job #471828)

Utilizator ChallengeMurtaza Alexandru Challenge Data 21 iulie 2010 12:48:04
Problema Floyd-Warshall/Roy-Floyd Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <fstream>

using namespace std;

const char InFile[]="royfloyd.in";
const char OutFile[]="royfloyd.out";
const int MaxN=105;
const int INF=1<<30;

ifstream fin(InFile);
ofstream fout(OutFile);

int N,path_cost[MaxN][MaxN],edge_cost[MaxN][MaxN];

inline int int_min(int a, int b)
{
	if(a<b)return a;
	return b;
}

inline void royfloyd()
{
	for(register int i=1;i<=N;++i)
	{
		for(register int j=1;j<=N;++j)
		{
			path_cost[i][j]=edge_cost[i][j];
		}
	}

	for(register int k=1;k<=N;++k)
	{
		for(register int i=1;i<=N;++i)
		{
			for(register int j=1;j<=N;++j)
			{
				path_cost[i][j]=int_min(path_cost[i][j],path_cost[i][k]+path_cost[k][j]);
			}
		}
	}
}

int main()
{
	fin>>N;
	for(register int i=1;i<=N;++i)
	{
		for(register int j=1;j<=N;++j)
		{
			fin>>edge_cost[i][j];
			if(!edge_cost[i][j])
			{
				edge_cost[i][j]=INF;
			}
		}
		edge_cost[i][i]=0;
	}
	fin.close();

	royfloyd();

	for(register int i=1;i<=N;++i)
	{
		for(register int j=1;j<=N;++j)
		{
			fout<<path_cost[i][j]<<" ";
		}
		fout<<"\n";
	}
	fout.close();
	return 0;
}