Cod sursa(job #2863592)

Utilizator the_horoHorodniceanu Andrei the_horo Data 6 martie 2022 22:15:16
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <algorithm>
#include <array>
#include <cstdint>
#include <fstream>

using u32 = uint32_t;
using u64 = uint64_t;

constexpr u32 MAX_NODES = 100;
constexpr u64 MAX_PATH  = 1000000000;


std::array<std::array<u64, MAX_NODES>, MAX_NODES> mat;

int main () {
    std::ifstream f("royfloyd.in");
    f.exceptions(f.badbit | f.failbit | f.eofbit);
    std::ofstream g("royfloyd.out");

    size_t n;
    f >> n;

    for (size_t i = 0; i != n; ++ i)
        for (size_t j = 0; j != n; ++ j) {
            f >> mat[i][j];
            if (mat[i][j] == 0 && i != j)
                mat[i][j] = MAX_PATH;
        }

    for (size_t k = 0; k != n; ++ k)
        for (size_t i = 0; i != n; ++ i)
            for (size_t j = 0; j != n; ++ j)
                mat[i][j] = std::min(mat[i][j], mat[i][k] + mat[k][j]);

    for (size_t i = 0; i != n; ++ i, g << '\n')
        for (size_t j = 0; j != n; ++ j)
            g << (mat[i][j] == MAX_PATH ? 0 : mat[i][j]) << ' ';
}