Cod sursa(job #3302955)

Utilizator Iustin_Mircea2010Iustin Mircea Iustin_Mircea2010 Data 12 iulie 2025 12:47:26
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;

const int inf = 1e9;

int dp[105][105];

int main(){
    
    ifstream cin("royfloyd.in");
    ofstream cout("royfloyd.out");
    
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            cin >> dp[i][j];
            if(dp[i][j] == 0 && i != j) dp[i][j] = inf;
        }
    }
    for(int k = 1; k <= n; k++){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
            }
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(dp[i][j] == inf || i == j) cout << 0 << " ";
            else cout << dp[i][j] << " ";
        }
        cout << '\n';
    }
    return 0;
}