Cod sursa(job #3193701)

Utilizator Giulian617Buzatu Giulian Giulian617 Data 15 ianuarie 2024 14:25:54
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include<bits/stdc++.h>
using namespace std;
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");
const int NMAX=105,INF=0x3F3F3F3F;
int n,x,d[NMAX][NMAX];
int main()
{
    f>>n;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
        {
            f>>x;
            d[i][j]=x;
            if(i!=j && d[i][j]==0)/// we don't have an edge between i and j
                d[i][j]=INF;/// hence we have the distance equal to INF
        }

    for(int k=1; k<=n; k++)
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                d[i][j]=min(d[i][j],d[i][k]+d[k][j]);

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            if(d[i][j]==INF)
                g<<0<<' ';
            else
                g<<d[i][j]<<' ';
        g<<'\n';
    }
    return 0;
}