Cod sursa(job #2335422)

Utilizator LucaSeriSeritan Luca LucaSeri Data 4 februarie 2019 08:15:03
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 110;

int mat[MAXN][MAXN];

int main() {
	#ifdef BLAT
		freopen("input", "r", stdin);
	#else
		freopen("royfloyd.in", "r", stdin);
		freopen("royfloyd.out", "w", stdout);
	#endif

	int n;
	scanf("%d", &n);

	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= n; ++j) {
			scanf("%d", &mat[i][j]);
		} 
	}

	for(int k = 1; k <= n; ++k) {
		for(int i = 1; i <= n; ++i) {
			for(int j = 1; j <= n; ++j) {
				if(i == j) continue;
				if(mat[i][k] && mat[k][j] && (mat[i][k] + mat[k][j] < mat[i][j] || !mat[i][j])) mat[i][j] = mat[i][k] + mat[k][j];
			}
		}
	}
	
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= n; ++j) {
			printf("%d ", mat[i][j]);
		} 

		printf("\n");
	}
}