Cod sursa(job #492087)

Utilizator AndrewTheGreatAndrei Alexandrescu AndrewTheGreat Data 13 octombrie 2010 13:45:38
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <stdio.h>
#define INF 0xf3f3f3f
#define Max 110

using namespace std;

int a[Max][Max], N;

void read()
{
    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]);
            if(!a[i][j])
                a[i][j] = INF;
        }
}

void royfloyd()
{
    int t;
    for(int k = 1; k <= N; k++)
        for(int i = 1; i <= N; i++)
            for(int j = 1; j <= N; j++)
            a[i][j] = (a[i][j] > (t = a[i][k] + a[k][j]))?t:a[i][j];

}

void write()
{
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
            if(a[i][j] == INF || i == j)
                printf("0 ");
            else printf("%d ",a[i][j]);
        printf("\n");
    }
}

int main()
{
    read();
    royfloyd();
    write();
    return 0;
}