Cod sursa(job #1425182)

Utilizator alex.bullzAlexandru Lilian alex.bullz Data 26 aprilie 2015 22:20:40
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <fstream>

#define MAX ((1 << 29) - 1)

int min (int a, int b) { return a < b ? a : b; }

int cost[100][100];

int main() {
    std::ifstream ("royfloyd.in");
    std::ofstream ("royfloyd.out");
    int dimension, element;

    fin >> dimension;

    for (int i = 0; i < dimension; ++i) {
        for (int j = 0; j < dimension; ++j) {
            fin >> element;
            if (element == 0 && i != j) {
                cost[i][j] = MAX;
            } else {
                cost[i][j] = element;
            }
        }
    }

    for (int k = 0; k < dimension; ++k) {
        for (int i = 0; i < dimension; ++i) {
            for (int j = 0; j < dimension; ++j) {
                cost[i][j] = min (cost[i][j], cost[i][k] + cost[k][j]);
            }
        }
    }

    for (int i = 0; i < dimension; ++i) {
        for (int j = 0; j < dimension; ++j) {
            fout << cost[i][j] << " ";
        }
        fout << "\n";
    }
}