Pagini recente » Cod sursa (job #2543343) | Cod sursa (job #3338144) | Monitorul de evaluare | Cod sursa (job #3343781) | Cod sursa (job #3357861)
#include <fstream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
ifstream cin("royfloyd.in");
ofstream cout("royfloyd.out");
int n;
int dist[105][105];
const int inf = 1e9;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> dist[i][j];
if (dist[i][j] == 0 && 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++) {
if (dist[i][k] < inf && dist[k][j] < inf) {
if (dist[i][j] > dist[i][k] + dist[k][j]) {
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;
}