Cod sursa(job #1887332)

Utilizator 1475369147896537415369Andrei Udriste 1475369147896537415369 Data 21 februarie 2017 15:36:52
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <cstdio>
using namespace std;
#define INF 1001

int vertices, adjMatrix[101][101];

int main(){

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

scanf("%d", &vertices);

for(int row = 1; row <= vertices; row++){
    for(int column = 1; column <= vertices; column++){
        scanf("%d", &adjMatrix[row][column]);
        if(adjMatrix[row][column] == 0 && row != column){
            adjMatrix[row][column] = INF;
        }
    }
}
for(int k = 1; k <= vertices; k++){
    for(int i = 1; i <= vertices; i++){
        for(int j = 1; j <= vertices; j++){
            if(adjMatrix[i][j] > adjMatrix[i][k] + adjMatrix[k][j]){
                adjMatrix[i][j] = adjMatrix[i][k] + adjMatrix[k][j];
            }
        }
    }
}
for(int row = 1; row <= vertices; row++){
    for(int column = 1; column <= vertices; column++){
        printf("%d ", (adjMatrix[row][column] == INF) ? 0 : adjMatrix[row][column]);
    }printf("\n");
}
return 0;
}