Cod sursa(job #3246435)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 3 octombrie 2024 00:01:32
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <fstream>

using namespace std;

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

static constexpr int nMAX = ((int)(1e3) + 1);

int d[nMAX][nMAX];

inline int my_min(const int x, const int y)
{
    return ((x < y) ? x : y);
}

int main()
{
    int n = 0;
    f >> n;

    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            f >> d[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 != j)
                    if (i != k)
                        if (j != k)
                            if (d[i][k] && d[k][j])
                            {
                                if (d[i][j] == 0)
                                    d[i][j] = d[i][k] + d[k][j];
                                else
                                    d[i][j] = my_min(d[i][j], (d[i][k] + d[k][j]));
                            }

    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= n; ++j)
        {
            g << d[i][j];

            if (j != n)
                g << ' ';
            else
                g << '\n';
        }
    }

    return 0;
}