Cod sursa(job #2979888)

Utilizator suzanicaSuzanica Mihu suzanica Data 16 februarie 2023 08:25:11
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 f("royfloyd.in");
ofstream g("royfloyd.out");
const int N = 101;
const int oo = 1000001;
int n,d[N][N];
int main()
{
    f>>n;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
        {
            int x;
            f>>x;
            if(x==0)
                x=oo;
            d[i][j]=x;
        }
    for(int z=1; z<=n; z++)
        for(int x=1; x<=n; x++)
            for(int y=1; y<=n; y++)
                if(x!=y && x!=z && y!=z)
                    if(d[x][y]>d[x][z]+d[z][y])
                        d[x][y]=d[x][z]+d[z][y];
    for(int i=1; i<=n; i++,g<<'\n')
        for(int j=1; j<=n; j++,g<<' ')
        {
            if(d[i][j]==oo)
                d[i][j]=0;
            g<<d[i][j];
        }
    return 0;
}