Cod sursa(job #3168109)

Utilizator not_anduAndu Scheusan not_andu Data 11 noiembrie 2023 16:08:40
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <bits/stdc++.h>
#pragma GCC optimize ("O3");

using namespace std;

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

const short VMAX = 105;
const int INF = 1e9;

short n;
int v[VMAX][VMAX], d[VMAX][VMAX];

void solve(){

    cin >> n;

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

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

    for(int k = 1; k <= n; ++k){
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n; ++j){
                if(i != j){
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
                }
            }
        }
    }

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

}

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