Cod sursa(job #2374105)

Utilizator RaduMirceaAndreiRadu Mircea Andrei RaduMirceaAndrei Data 7 martie 2019 16:58:38
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
# include <fstream>
# define DIM 105
# define INF 1000000000
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
int v[DIM][DIM],n,m,k,i,j;
int main () {
    fin>>n;
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            fin>>v[i][j];
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            if(v[i][j]==0)
                v[i][j]=INF;
    for(k=1;k<=n;k++)
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                if(i!=j&&i!=k&&j!=k&&v[i][j]>v[i][k]+v[k][j])
                    v[i][j]=v[i][k]+v[k][j];
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            if(v[i][j]==INF)
                v[i][j]=0;
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++)
            fout<<v[i][j]<<" ";
        fout<<"\n";
    }
    return 0;
}