Cod sursa(job #3251172)

Utilizator Xutzu358Ignat Alex Xutzu358 Data 25 octombrie 2024 11:19:41
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include <climits>
using namespace std;

int dist[105][105];
int n;
int inf = 1000000001;

int main() {
    freopen("royfloyd.in", "r", stdin);
    freopen("royfloyd.out", "w", stdout);
    cin >> n;
    for (int i=1;i<=n;i++) {
        for (int j=1;j<=n;j++) {
            cin >> dist[i][j];
            dist[i][j] = (dist[i][j] != 0 ? 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 (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++) {
            cout << (dist[i][j] != inf ? dist[i][j] : 0) << " ";
        }
        cout << '\n';
    }
    return 0;
}