Cod sursa(job #894873)

Utilizator mmanMihai Manolescu mman Data 27 februarie 2013 00:35:28
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include<fstream>
#define dmax 102
using namespace std;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");

int n, cost[dmax][dmax], dist[dmax][dmax];

int main()
{
	in>>n;
	
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++)
		{	
			in>>cost[i][j];
			dist[i][j] = cost[i][j];
		}	
		
	in.close();

	for(int k=1; k<=n; k++)
		for(int i=1; i<=n; i++)
			for(int j=1; j<=n; j++)
				if(dist[i][k] && dist[k][j] && i!=j)
					if(dist[i][k] + dist[k][j] < dist[i][j] || dist[i][j] == 0)
						dist[i][j] = dist[i][k] + dist[k][j];
					
	for(int i=1; i<=n; i++)
	{	
		for(int j=1; j<=n; j++)	
			out<<dist[i][j]<<" ";
		out<<'\n';
	}	
	
	out.close();
	return 0;
}