Cod sursa(job #1706513)

Utilizator preda.andreiPreda Andrei preda.andrei Data 22 mai 2016 18:35:11
Problema Floyd-Warshall/Roy-Floyd Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <iostream>
#include <fstream>

using namespace std;

#define N_MAX 100

int costuri[N_MAX + 1][N_MAX + 1];

int main()
{
    ios::sync_with_stdio(false);
    ifstream fin("royfloyd.in");
    ofstream fout("royfloyd.out");

    int n;
    fin >> n;

    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            fin >> costuri[i][j];

    for(int k = 1; k <= n; ++k){
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n; ++j){
                if(i != k && i != j && k != j && costuri[i][k] + costuri[k][j] > 0){
                    if(costuri[i][j] > costuri[i][k] + costuri[k][j] || costuri[i][j] == 0)
                        costuri[i][j] = costuri[i][k] + costuri[k][j];
                }
            }
        }
    }

    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j)
            fout << costuri[i][j] << " ";
        fout << "\n";
    }

    return 0;
}