Cod sursa(job #1211157)

Utilizator anarogozAna Rogoz anarogoz Data 22 iulie 2014 02:18:24
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include<cstdio>
using namespace std;
const int NMAX = 100;
int dist[NMAX+1][NMAX+1], inf = 2000000;
int main()
{
    freopen("royfloyd.in", "r", stdin);
    freopen("royfloyd.out", "w", stdout);
    int n, i, j, k;
    scanf("%d", &n);
    for(i = 1; i <= n; i++)
        for(j = 1; j <= n; j++) {
            scanf("%d", &dist[i][j]);
            if(i!=j&&dist[i][j]==0)
                dist[i][j]=inf;
        }
    for(k = 1; k <= n; k++)
        for(i = 1; i <= n; i++)
            for(j = 1; j <=n; j++)
                if(dist[i][j] > dist[i][k] + dist[k][j])
                    dist[i][j] = dist[i][k] + dist[k][j];

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