Cod sursa(job #2954368)

Utilizator AleXutzZuDavid Alex Robert AleXutzZu Data 14 decembrie 2022 08:11:32
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 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] = {0};

    for (auto &i: shortest_path) {
        for (int &j: i) {
            j = INT32_MAX;
        }
    }

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

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

    for (int k = 1; k <= n; ++k) {
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (shortest_path[i][k] + shortest_path[k][j] > -1) {
                    if (shortest_path[i][j] > shortest_path[i][k] + shortest_path[k][j]) {
                        shortest_path[i][j] = shortest_path[i][k] + shortest_path[k][j];
                    }
                }
            }
        }
    }

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

    return 0;
}