Cod sursa(job #3196887)

Utilizator Vlad_NistorNIstor Vlad Vlad_Nistor Data 24 ianuarie 2024 22:02:34
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <bits/stdc++.h>
using namespace std;
#define INF 2000
#define NMAX 120
int a[NMAX][NMAX]; /// weights of the vertices from i to j
int main(void){
    ofstream cout("royfloyd.out");
    ifstream cin("royfloyd.in");
    int n;
    cin >> n;
    for(int i= 1;i<=n;i++){
        for(int j= 1;j<=n;j++){
            cin >> a[i][j];
            if(a[i][j] == 0 && i != j){
                a[i][j] = INF;
            }
        }
    }
    for(int k = 1;k<=n;++k){
        for(int i = 1;i<=n;i++){
            if(i != k){
                for(int j = 1;j<=n;j++){
                    if(j != k && j != i){
                        if(a[i][j] > a[i][k] + a[k][j]){
                            a[i][j] = a[i][k] + a[k][j];
                        }
                    }
                }
            }
        }
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=n;j++){
            if(a[i][j] == INF)cout << 0 << ' ';
            else cout << a[i][j] << ' ';
        }
        cout << '\n';
    }
}