Cod sursa(job #3261478)

Utilizator AlexPlesescuAlexPlesescu AlexPlesescu Data 6 decembrie 2024 11:03:27
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <bits/stdc++.h>

using namespace std;
#define int long long int
#define pb push_back
#define sz(x)(int)(x.size())
const int N = 105, INF = 1e9 + 7;

int n, a[N][N], dp[N][N];

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    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 >> a[i][j];
            dp[i][j] = INF;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (a[i][j] != 0) {
                dp[i][j] = a[i][j];
            } else {
                dp[i][j] = INF;
            }
        }
    }
    for (int z = 1; z <= n; z++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (i != j && dp[i][z] != INF && dp[z][j] != INF) {
                    dp[i][j] = min(dp[i][j], dp[i][z] + dp[z][j]);
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (dp[i][j] == INF)
                cout << 0 << ' ';
            else
                cout << dp[i][j] << ' ';
        }
        cout << '\n';
    }
}