Pagini recente » Cod sursa (job #2802763) | Cod sursa (job #892102) | Cod sursa (job #1491228) | Cod sursa (job #600406) | Cod sursa (job #3251482)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");
int inf = 1000000000;
int n; /// n - nr noduri
int dist[1005][1005]; /// matrice cu distante
int main() {
f >> n;
for (int i=1;i<=n;i++) {
for (int j=1;j<=n;j++) {
f >> dist[i][j];
if (dist[i][j] == 0)
dist[i][j] = inf;
/// dist[i][j] = inf => nu exista muchie intre i si j
/// dist[i][j] = 0 <=> i == j (depinde de la caz la caz)
/// dist[i][j] != 0 => exista muchie intre i si j
}
}
for (int k=1;k<=n;k++) {
for (int i=1;i<=n;i++) {
for (int j=1;j<=n;j++) {
/// incercam sa actualizam dist (i,j) trecand prin nodul k
if (i != j)
dist[i][j] = min(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) g << 0 << " ";
else g << dist[i][j] << " ";
}
g << '\n';
}
return 0;
}