Cod sursa(job #3351463)

Utilizator Alexutu008Ionita Alexandru-Dumitru Alexutu008 Data 19 aprilie 2026 16:07:21
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;

int n, a[101][101];

void rf() {
	for (int k = 1; k <= n; ++k) {
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j) {
				if (a[i][k] != 0 && a[k][j] != 0 && (a[i][j] > a[i][k] + a[k][j] || a[i][j] == 0) && i != j) {
					a[i][j] = a[i][k] + a[k][j];
				}
			}
		}
	}
}

int main() {
	freopen("royfloyd.in", "r", stdin);
	freopen("royfloyd.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);

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

	rf();

	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			cout << a[i][j] << ' ';
		}
		cout << '\n';
	}

	return 0;
}