Cod sursa(job #1983938)

Utilizator Dragne.Andrei11Dragne Andrei Dragne.Andrei11 Data 22 mai 2017 22:15:25
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <bits/stdc++.h>
#define nmax 101
#define inf 1001

using namespace std;

int ma[nmax][nmax];
int main()
{
    freopen("royfloyd.in", "r", stdin);
    freopen("royfloyd.out", "w", stdout);
    int n;

    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>ma[i][j];
            if(ma[i][j]==0)
                ma[i][j]=inf;
        }
    }
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                ma[i][j]=min(ma[i][j], ma[i][k]+ma[k][j]);
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j)
                cout<<"0"<<' ';
            else if(ma[i][j]==inf)
                cout<<"0"<<' ';
            else
                cout<<ma[i][j]<<' ';
        }
        cout<<'\n';
    }

    return 0;
}