Cod sursa(job #1579731)

Utilizator lflorin29Florin Laiu lflorin29 Data 25 ianuarie 2016 00:34:13
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <bits/stdc++.h>

using namespace std;

const int N = 128;

int n;
int a[N][N];

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

    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            scanf("%d", &a[i][j]);
    for(int act = 1; act <= n; ++act)
        for(int i = 1; i <= n; ++i)
           for(int j = 1; j <= n; ++j)
               if(i != j and a[act][j] and a[i][act]) {
                  bool Ok = (a[act][j] + a[i][act]) < a[i][j];
                  if(Ok || a[i][j] == 0) a[i][j] = a[act][j] + a[i][act];
               }

    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= n; ++j)
            printf("%d ", a[i][j]);
        printf("\n");
    }
    return 0;
}