Mai intai trebuie sa te autentifici.
Cod sursa(job #3336699)
| Utilizator | Data | 25 ianuarie 2026 13:41:26 | |
|---|---|---|---|
| Problema | Floyd-Warshall/Roy-Floyd | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.97 kb |
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> adj;
const int INF = 2e9;
void royfloyd(int n) {
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (adj[i][k] != INF && adj[k][j] != INF)
adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]);
}
int main() {
freopen("royfloyd.in", "r", stdin);
freopen("royfloyd.out", "w", stdout);
int n;
cin >> n;
adj.assign(n + 1, vector<int>(n + 1, INF));
for (int i = 1; i < n + 1; i++) {
for (int j = 1; j < n + 1; j++) {
int w; cin >> w;
if (w == 0 && i != j) continue;
adj[i][j] = w;
}
}
royfloyd(n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (adj[i][j] == INF)
cout << 0 << " ";
else
cout << adj[i][j] << " ";
}
cout << "\n";
}
return 0;
}