Cod sursa(job #2768997)

Utilizator Razvan48Capatina Razvan Nicolae Razvan48 Data 12 august 2021 22:35:39
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <fstream>

using namespace std;

const int INF = 1000000000;
const int NMAX = 100;

int mat[1 + NMAX][1 + NMAX];

int sol[1 + NMAX][1 + NMAX];

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

    int n;
    in >> n;

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            in >> mat[i][j];

            if (i == j)
                sol[i][j] = 0;
            else if (mat[i][j])
                sol[i][j] = mat[i][j];
            else
                sol[i][j] = INF;
        }

    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                sol[i][j] = min(sol[i][j], sol[i][k] + sol[k][j]);

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (sol[i][j] == INF)
                sol[i][j] = 0;

            out << sol[i][j] << ' ';
        }
        out << '\n';
    }

    return 0;
}