Cod sursa(job #2719449)

Utilizator Botnaru_VictorBotnaru Victor Botnaru_Victor Data 9 martie 2021 21:01:54
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>

#define nmax 101
#define valmax 1001

using namespace std;

int edge[nmax][nmax];
int roy[nmax][nmax];
int n;

int main()
{
    freopen("royfloyd.in","r",stdin);
    freopen("royfloyd.out","w",stdout);

    cin>>n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>edge[i][j];
            if(edge[i][j]==0&&i!=j) roy[i][j]=valmax;
            else roy[i][j]=edge[i][j];
        }
    }

    for(int k=0;k<n;k++)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(roy[i][j]>roy[i][k]+roy[k][j]) roy[i][j]=roy[i][k]+roy[k][j];
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(roy[i][j]>=valmax) cout<<0<<' ';
            else cout<<roy[i][j]<<' ';
        }
        cout<<'\n';
    }
    return 0;
}