Cod sursa(job #2355624)

Utilizator mihai.alphamihai craciun mihai.alpha Data 26 februarie 2019 10:46:55
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <bits/stdc++.h>

using namespace std;

int d[105][105];
const int INF = 1e9;

int main()  {
    ifstream cin("royfloyd.in");
    ofstream cout("royfloyd.out");
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++)
        for(int j = 1;j <= n;j++)  {
            cin >> d[i][j];
            if(d[i][j] == 0)
                d[i][j] = INF;
        }
    for(int k = 1;k <= n;k++)
        for(int i = 1;i <= n;i++)
            for(int j = 1;j <= n;j++)  {
                if(k != i && k != j && i != j)
                    d[i][j] = min(d[i][k] + d[k][j], d[i][j]);
            }
    for(int i = 1;i <= n;i++)  {
        for(int j = 1;j <= n;j++)  {
            if(d[i][j] != INF)
                cout << d[i][j] << " ";
            else
                cout << "0 ";
        }
        cout << "\n";
    }
    return 0;
}