Cod sursa(job #2643678)

Utilizator De_Azi_Ne_DopamAlex Ardelean De_Azi_Ne_Dopam Data 20 august 2020 20:39:27
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;
int d[101][101];
int main()
{
#ifdef HOME
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
#endif // HOME
#ifndef HOME
    freopen("royfloyd.in","r",stdin);
    freopen("royfloyd.out","w",stdout);
#endif // HOME
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n,i,j;
    cin>>n;
    for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
            cin>>d[i][j];
    for(int k=1; k<=n; k++)
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
                if(i!=j&&d[i][k]&&d[k][j]&&(d[i][k]+d[k][j]<d[i][j]||d[i][j]==0))
                    d[i][j]=d[i][k]+d[k][j];
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=n; j++)
            cout<<d[i][j]<<" ";
        cout<<'\n';
    }
    return 0;
}