Cod sursa(job #3185214)

Utilizator not_anduAndu Scheusan not_andu Data 18 decembrie 2023 14:35:50
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <bits/stdc++.h>

using namespace std;

#define INFILE "royfloyd.in"
#define OUTFILE "royfloyd.out"

typedef long long ll;

const int N_MAX = 105;
const int INF = 0x3f3f3f3f;

int n;
ll d[N_MAX][N_MAX];

void solve(){

    cin >> n;

    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            cin >> d[i][j];
            if(i != j && d[i][j] == 0) d[i][j] = INF;
        }
    }

    for(int nodeAux = 1; nodeAux <= n; ++nodeAux){
        for(int node = 1; node <= n; ++node){
            for(int to = 1; to <= n; ++to){
                if(node != to){
                    d[node][to] = min(d[node][to], d[node][nodeAux] + d[nodeAux][to]);
                }
            }
        }
    }

    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            cout << d[i][j] << " ";
        }
        cout << '\n';
    }

}

int main(){
    ios_base::sync_with_stdio(false);
    freopen(INFILE, "r", stdin);
    freopen(OUTFILE, "w", stdout);
    cin.tie(0), cout.tie(0);
    solve();
    return 0;
}