Cod sursa(job #1713640)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 6 iunie 2016 03:59:53
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <bits/stdc++.h>

#define INF 0x3f3f3f3f

using namespace std;
int M[105][105],DP[2][105][105],N;

void Read()
{
    scanf("%d",&N);
    for(int i = 1; i <= N; ++i)
        for(int j = 1; j <= N; ++j){
            scanf("%d",&M[i][j]);
            if(M[i][j] == 0)
                M[i][j] = INF;
        }
}

void FW()
{
    memcpy(DP[0],M,sizeof(M));
    int crt = 1,ant = 0;
    for(int k = 1; k <= N; ++k)
    {
        for(int i = 1; i <= N; ++i)
            for(int j = 1; j <= N; ++j)
                    DP[crt][i][j] = min(DP[ant][i][k] + DP[ant][k][j], DP[ant][i][j]);
        crt ^= 1;
        ant ^= 1;
    }
    for(int i = 1; i <= N; ++i)
    {
        for(int j = 1; j <= N; ++j)
            if(DP[ant][i][j] >= INF || i == j)
                printf("0 ");
            else
                printf("%d ",DP[ant][i][j]);
        printf("\n");
    }
}

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

    Read();
    FW();

    return 0;
}