Cod sursa(job #2867066)

Utilizator the_horoHorodniceanu Andrei the_horo Data 10 martie 2022 10:38:10
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <array>
#include <cstdint>
#include <fstream>

using u32 = uint32_t;

constexpr u32 INF_PATH = 0x7fffffff;
constexpr size_t MAX_NODES = 100;

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

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

    size_t nodes;
    in >> nodes;

    for (size_t i = 0; i != nodes; ++ i)
        for (size_t j = 0; j != nodes; ++ j) {
            in >> mat[i][j];
            if (i != j && mat[i][j] == 0)
                mat[i][j] = INF_PATH;
        }

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

    for (size_t i = 0; i != nodes; ++ i, out.put('\n'))
        for (size_t j = 0; j != nodes; ++ j)
            out << mat[i][j] << ' ';
}