Cod sursa(job #2466794)

Utilizator educationalLets Go educational Data 2 octombrie 2019 22:47:23
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>
#define MaxN 100
using namespace std;

int best[MaxN + 1][MaxN + 1];
int N;

int main(){

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

    int i, j, k;

    cin >> N;
    for(i = 1; i <= N; i++)
        for(j = 1; j <= N; j++)
        cin >> best[i][j];

    for(i = 1; i <= N; i++)
        for(j = 1; j <= N; j++)
        if(!best[i][j]) best[i][j] = 1 << 29;

    for(k = 1; k <= N; k++)
        for(i = 1; i <= N; i++)
            for(j = 1; j <= N; j++)
            if(i != j) best[i][j] = min(best[i][j], best[i][k] + best[k][j]);

    for(i = 1; i <= N; i++)
    {
        for(j = 1; j <= N; j++)
            if(best[i][j] == 1 << 29) cout << 0 << ' ';
            else cout << best[i][j] << ' ';
        cout << '\n';
    }



return 0;
}