Cod sursa(job #891822)

Utilizator howsiweiHow Si Wei howsiwei Data 25 februarie 2013 20:26:04
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
	ifstream fin("royfloyd.in");
	ofstream fout("royfloyd.out");
	int n; fin >> n;
	vector<vector<int> > dist(n, vector<int>(n, 1<<21));
	for (int i=0; i<n; ++i) {
		for (int j=0; j<n; ++j) {
			fin >> dist[i][j];
			if (i!=j && dist[i][j]==0)
				dist[i][j]=1<<21;
		}
	}
	for (int k=0; k<n; ++k)
		for (int i=0; i<n; ++i)
			for (int j=0; j<n; ++j)
				dist[i][j] = min( dist[i][j], dist[i][k] + dist[k][j] );
	for (int i=0; i<n; ++i) {
		for (int j=0; j<n; ++j)
			fout << (dist[i][j]>(1<<20) ?0 :dist[i][j]) << ' ';
		fout << '\n';
	}
	return 0;
}