Cod sursa(job #1454354)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 26 iunie 2015 11:28:59
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <vector>
#include <limits>
using namespace std;

constexpr int inf = numeric_limits<int>::max();

int main(){
	ifstream f("royfloyd.in");
	ofstream g("royfloyd.out");
	int n;
	f >> n;
	vector<vector<int> > dist(n, vector<int>(n));
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j){
			f >> dist[i][j];
			if(dist[i][j] == 0){
				dist[i][j] = inf; } } }
	for(int k = 0; k < n; ++k){
		for(int i = 0; i < n; ++i){
			for(int j = 0; j < n; ++j){
				if(i!=j && dist[i][k] != inf && dist[k][j] != inf){
					dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } }
	for(const auto& r : dist){
		for(const auto y : r){
			g << (y==inf ? 0 : y) << ' '; }
		g << '\n'; }
	return 0; }