Cod sursa(job #2999621)

Utilizator raulboancaBoanca Raul Alin raulboanca Data 11 martie 2023 11:12:40
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>

using namespace std;

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

int mat[105][105],n,m,cost[105][105],pret;
const int inf=2000000001;

void citire()
{
    fin>>n;
    int x,y;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            fin>>pret;
            if(pret!=0)
            cost[i][j]=pret;
            else if(i==j)
                cost[i][j]=0;
            else cost[i][j]=inf;
        }
    }
}

void roy()
{
    for(int k=1; k<=n; k++)
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(cost[i][k]!=inf && cost[k][j]!=inf && cost[i][k]+cost[k][j]<cost[i][j])
                    cost[i][j]=cost[i][k]+cost[k][j];
            }
        }
    }
}

int main()
{
    citire();
    roy();
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
            {
                if(cost[i][j]==inf)
                    fout<<0<<" ";
                else fout<<cost[i][j]<<" ";
            }
        fout<<'\n';
    }
    return 0;
}