Cod sursa(job #1121395)

Utilizator h2g2Ford Prefect h2g2 Data 25 februarie 2014 12:44:36
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <iostream>
#include <fstream>
#define nmax 105
#define inf 100000
using namespace std;

int n, best[nmax][nmax];

int main() {
	ifstream f("royfloyd.in");
	ofstream g("royfloyd.out");

	f>>n;
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++) {
			f>>best[i][j];
			if(best[i][j] == 0) best[i][j] = inf;
		}

	for(int k=1; k<=n; k++)
		for(int i=1; i<=n; i++)
			for(int j=1; j<=n; j++)
				best[i][j] = min(best[i][j], best[i][k] + best[k][j]);

	for(int i=1; i<=n; i++) {
		for(int j=1; j<=n; j++) g<<(best[i][j]==inf || i==j? 0:best[i][j])<<" ";
		g<<"\n";
	}

	return 0;
}