Cod sursa(job #1103948)

Utilizator diana97Diana Ghinea diana97 Data 10 februarie 2014 10:08:28
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <fstream>
using namespace std;

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

const int inf = 999999;
int n, cost[101][101];

void citeste ()
{
    f >> n;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
    {
        f >> cost[i][j];
        if (i != j && cost[i][j] == 0) cost[i][j]=inf;
    }
}

void royfloyd ()
{
    int k, i, j;
    for (k = 1; k <= n; k++)
        for (i = 1; i <= n; i++)
            for (j = 1; j <=n ; j++)
                cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
}

void scrieMatrice ()
{
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
            if (cost[i][j] == inf) g << 0 << ' ';
            else g << cost[i][j]<<' ';
        g<<'\n';
    }

}

int main()
{
    citeste ();
    royfloyd ();
    scrieMatrice ();
    return 0;
}