Cod sursa(job #2954366)

Utilizator AleXutzZuDavid Alex Robert AleXutzZu Data 14 decembrie 2022 07:51:53
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <iostream>
#include <fstream>

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

    int graph[101][101] = {0};
    int n;
    input >> n;

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            input >> graph[i][j];
        }
    }

    int shortest_path[101][101][101] = {0};

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            shortest_path[i][j][0] = graph[i][j];
        }
    }

    for (int k = 1; k <= n; ++k) {
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                shortest_path[i][j][k] = std::min(shortest_path[i][j][k - 1],
                                                  shortest_path[i][k][k - 1] + shortest_path[k][j][k - 1]);
            }
        }
    }

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            output << shortest_path[i][j][n] << " ";
        }
        output << '\n';
    }

    return 0;
}