Cod sursa(job #3251497)

Utilizator lucatanasegLuca-Tanase Grindean lucatanaseg Data 26 octombrie 2024 09:35:49
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");

int n;
long long dist[1005][1005];
int main()
{
    fin>>n;
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            int x;
            fin>>x;
            if(x==0) x=INT_MAX;
            dist[i][j]=x;
        }
    }
    for(int k=1; k<=n; k++){
        for(int i=1; i<=n; i++){
            for(int j=1; j<=n; j++){
                if(i!=j){
                    dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j]);
                }
            }
        }
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            if(dist[i][j]==INT_MAX) fout<<0<<' ';
            else fout<<dist[i][j]<<' ';
        }
        fout<<'\n';
    }

    return 0;
}