Cod sursa(job #1115133)

Utilizator andreiagAndrei Galusca andreiag Data 21 februarie 2014 20:18:49
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>

using namespace std;

const int Nmax = 100;
const int INF = 100000000;

int G[Nmax][Nmax];

int main()
{
    ifstream f ("royfloyd.in");
    ofstream g ("royfloyd.out");

    int N;
    f >> N;
    for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
    {
        f >> G[i][j];
        if (i!=j && G[i][j] == 0) G[i][j] = INF;
    }

    for (int k = 0; k < N; k++)
    for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
    {
        if (G[i][j] > G[i][k] + G[k][j])
            G[i][j] = G[i][k] + G[k][j];
    }

    for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
    {
        if (G[i][j] == INF) g << 0;
        else g << G[i][j];

        if (j < N-1) g << ' ';
        else g << endl;
    }

    return 0;
}