Cod sursa(job #2913487)

Utilizator VladNANegoita Vlad-Andrei VladNA Data 14 iulie 2022 19:16:06
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <bits/stdc++.h>

using namespace std;

#define NMAX 101
#define INF 10000005

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

int adj[NMAX][NMAX];
int best[NMAX][NMAX];

int main()
{
    int n;
    in >> n;

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

    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j) {

            if (adj[i][j] == 0)
                adj[i][j] = INF;

            best[i][j] = adj[i][j];
        }

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

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j)
            if (adj[i][j] > 0 && i != j)
                out << best[i][j] << ' ';
            else
                out << 0 << ' ';
        out << '\n';
    }
    return 0;
}