Cod sursa(job #3357802)

Utilizator TestLicenta123Test Test TestLicenta123 Data 13 iunie 2026 15:04:48
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <iostream>
#include <fstream>
#include <climits>
#include <algorithm>

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

    int n;
    input >> n;

    int graph[101][101];

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            input >> graph[i][j];
            if (i != j && graph[i][j] == 0) {
                graph[i][j] = INT_MAX;
            }
        }
    }

    for (int k = 1; k <= n; ++k) {
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (graph[i][k] != INT_MAX && graph[k][j] != INT_MAX) {
                    graph[i][j] = std::min(graph[i][j], graph[i][k] + graph[k][j]);
                }
            }
        }
    }

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (graph[i][j] == INT_MAX) {
                output << "0 ";
            } else {
                output << graph[i][j] << " ";
            }
        }
        output << '\n';
    }

    return 0;
}