Cod sursa(job #2772571)

Utilizator cyg_dawidDavid Ghiberdic cyg_dawid Data 1 septembrie 2021 17:52:05
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
// roy floyd warshall pe graf orientat
#include <fstream>
#include <algorithm>

std::ifstream fin ("royfloyd.in");
std::ofstream fout ("royfloyd.out");

int const INF = 1e9;
int const nmax = 105;
int ans[nmax][nmax];

int main() {
    int n;
    fin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            int aux;
            fin >> aux;
            if (aux)
                ans[i][j] = aux;
            else
                ans[i][j] = INF;
        }
    }

    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                ans[i][j] = std::min(ans[i][j], ans[i][k] + ans[k][j]);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == j)
                fout << 0 << " ";
            else
                fout << ans[i][j] << " ";
        }
        fout << "\n";
    }
    return 0;
}