Cod sursa(job #1591561)

Utilizator AndreiGrigorasAndrei Grigoras AndreiGrigoras Data 6 februarie 2016 13:40:03
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>

#define NMax 110
#define INF 1000000000

using namespace std;

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

int nodes, dist[NMax][NMax];

int getMin(int a, int b)
{
    if (a < b)
        return a;
    return b;
}

int main()
{
    f >> nodes;
    for (int i = 1; i <= nodes; i++)
    {
        for (int j = 1; j <= nodes; j++)
        {
            f >> dist[i][j];

            if (!dist[i][j])
                dist[i][j] = INF;
        }
    }

    for (int k = 1; k <= nodes; k++)
    {
        for (int i = 1; i <= nodes; i++)
        {
            for (int j = 1; j <= nodes; j++)
            {
                if (i == j)
                    continue;

                dist[i][j] = getMin(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }

    for (int i = 1; i <= nodes; i++)
    {
        for (int j = 1; j <= nodes; j++)
            if (dist[i][j] != INF)
                g << dist[i][j] << " ";
            else
                g << 0 << " ";
        g << "\n";
    }
}