Cod sursa(job #3251485)

Utilizator cris_s2Cristina Arion cris_s2 Data 26 octombrie 2024 09:29:18
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
int n, dist[1001][1001];
int inf=10000000;
int main()
{   in>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            in>>dist[i][j];
            if(dist[i][j]==0){
                dist[i][j]=inf;
            }
        }
    }
    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]==inf) out<<"0 ";
            else  out<<dist[i][j]<<' ';
        }
        out<<endl;
    }
    return 0;
}