Cod sursa(job #865704)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 26 ianuarie 2013 21:00:06
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <iostream>
#include <cstdio>

using namespace std;

#define Nmax (1<<7)

int cost[Nmax][Nmax];

int N;

void citire(){

    freopen("royfloyd.in", "r", stdin);

    scanf("%d", &N);

    for(int i = 1; i <= N; i++)
        for(int j = 1; j <= N; j++)
            scanf("%d", &cost[i][j]);
}

void royfloyd(){

    for(int k = 1; k <= N; k++)
        for(int i = 1; i <= N; i++)
            for(int j = 1; j <= N; j++)
                if(( cost[i][j] > cost[i][k] + cost[k][j] || !cost[i][j]) &&
                    i != j && cost[i][k] && cost[k][j] )
                        cost[i][j] = cost[i][k] + cost[k][j];
}

void afis(){

    freopen("royfloyd.out", "w", stdout);

    for(int i = 1; i <= N; i++){
        for(int j = 1; j <= N; j++)
            printf("%d ", cost[i][j]);

        printf("\n");
    }
}

int main(){

    citire();
    royfloyd();
    afis();

    return 0;
}