Cod sursa(job #2725840)

Utilizator preda.andreiPreda Andrei preda.andrei Data 19 martie 2021 19:00:53
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>
#include <vector>

using namespace std;

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

    int n;
    fin >> n;

    constexpr auto kInf = 1 << 25;
    vector<vector<int>> cost(n, vector<int>(n));
    for (auto& row : cost) {
        for (auto& c : row) {
            fin >> c;
            if (c == 0) {
                c = kInf;
            }
        }
    }

    for (auto k = 0; k < n; k += 1) {
        for (auto i = 0; i < n; i += 1) {
            for (auto j = 0; j < n; j += 1) {
                if (i != j && i != k && j != k) {
                    cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
                }
            }
        }
    }

    for (const auto& row : cost) {
        for (const auto& c : row) {
            fout << (c < kInf ? c : 0) << " ";
        }
        fout << "\n";
    }

    return 0;
}