Cod sursa(job #1609875)

Utilizator BlueStrutAndrei Prahoveanu BlueStrut Data 23 februarie 2016 09:19:08
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include<cstdio>
using namespace std;
int i, j, n, k, a[105][105];
int main(){
    freopen("royfloyd.in","r",stdin);
    freopen("royfloyd.out","w",stdout);
    scanf("%d", &n);
    for (i=1;i<=n;i++) for (j=1;j<=n;j++) scanf("%d", &a[i][j]);
    for (k=1;k<=n;k++) for (i=1;i<=n;i++) for (j=1;j<=n;j++) {
        if ((a[i][k])&&(a[k][j])&&(i!=j)&&((a[i][j]>a[i][k]+a[k][j])||(!a[i][j])))
            a[i][j]=a[i][k]+a[k][j];
    }
    for (i=1;i<=n;i++) {
        for (j=1;j<=n;j++) printf("%d ", a[i][j]);
        printf("\n");
    }
    return 0;
}