Cod sursa(job #3325375)

Utilizator denis_cristeaCristea Denis-Adrian denis_cristea Data 25 noiembrie 2025 13:11:18
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <fstream>
#include <limits>

#define MAX_N 105

int dist[MAX_N][MAX_N];

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

    int n;
    fin >> n;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            fin >> dist[i][j];
            if (dist[i][j] == 0 && i != j) {
                dist[i][j] = std::numeric_limits<int>::max();
            }
        }
    }

    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (
                    dist[i][k] != std::numeric_limits<int>::max() &&
                    dist[k][j] != std::numeric_limits<int>::max() &&
                    dist[i][k] + dist[k][j] < dist[i][j]
                ) {
                    dist[i][j] = dist[i][k] + dist[k][j];
                }
            }
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (dist[i][j] == std::numeric_limits<int>::max()) {
                fout << 0;
            } else {
                fout << dist[i][j];
            }

            if (j < n) {
                fout << " ";
            }
        }
        fout << "\n";
    }

    return 0;
}