Cod sursa(job #2639480)

Utilizator KlinashkaDiacicov Calin Marian Klinashka Data 2 august 2020 13:14:50
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>
using namespace std;

const int INF=1e9, NMAX=101;
int dist[NMAX+1][NMAX+1];

int main() {
	freopen("royfloyd.in", "r", stdin);
	freopen("royfloyd.out", "w", stdout);
	int N;
	cin>>N;
	for(int i=1;i<=N;++i) {
		for(int j=1;j<=N;++j) {
			cin>>dist[i][j];
			if(!dist[i][j] && i!=j)
				dist[i][j]=INF;
		}
	}

	for(int k=1;k<=N;++k) {
		for(int i=1;i<=N;++i) {
			for(int j=1;j<=N;++j) {
				dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j]);
			}
		}
	}

	for(int i=1;i<=N;++i) {
		for(int j=1;j<=N;++j) {
			if(dist[i][j]>=INF)
				cout<<0;
			else
				cout<<dist[i][j]<<' ';
		}
		cout<<'\n';
	}

	return 0;
}