Cod sursa(job #2115365)

Utilizator DimaTCDima Trubca DimaTC Data 26 ianuarie 2018 17:47:44
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include<bits/stdc++.h>

using namespace std;

const int inf=2000000000;
int d[115][115];
int n,x;

int main() {
	ifstream cin("royfloyd.in");
	ofstream cout("royfloyd.out");
	cin>>n;
	
	for (int i=1; i<=n; i++) {
		for (int j=1; j<=n; j++) {
			cin>>x;
			if (i!=j && x==0) x=inf;
			d[i][j]=x;
		}
	}
	
	for (int k=1; k<=n; k++) {
		for (int i=1; i<=n; i++) {
			for (int j=1; j<=n; j++) {
				if (d[i][k]==inf || d[k][j]==inf) continue;
				d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
			}
		}
	}
	
	for (int i=1; i<=n; i++) {
		for (int j=1; j<=n; j++) {
			(d[i][j]==inf)?cout<<"0 ":cout<<d[i][j]<<" ";
		} cout<<'\n';
	}
	
	return 0;
}