Cod sursa(job #2409997)

Utilizator LucaSeriSeritan Luca LucaSeri Data 19 aprilie 2019 17:08:53
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 = 101;

int mat[MAXN][MAXN];

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

	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin >> n;

	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < n; ++j) cin >> mat[i][j];
	}

	for(int k = 0; k < n; ++k)
		for(int i = 0; i < n; ++i)
			for(int j = 0; j < n; ++j)
				if(mat[i][k] && mat[k][j] && (mat[i][k] + mat[k][j] < mat[i][j] || mat[i][j] == 0)) mat[i][j] = mat[i][k] + mat[k][j];	
	
	for(int i = 0; i < n; ++i) {
		mat[i][i] = 0;
		for(int j = 0; j < n; ++j) cout << mat[i][j] << ' ';
		cout << '\n';
	}
	return 0;
}