Mai intai trebuie sa te autentifici.
Cod sursa(job #3357861)
| Utilizator | Data | 13 iunie 2026 18:26:20 | |
|---|---|---|---|
| Problema | Floyd-Warshall/Roy-Floyd | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 1.13 kb |
#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;
}