Cod sursa(job #3336817)

Utilizator MihaisiatatPavel Mihai-George Mihaisiatat Data 26 ianuarie 2026 01:53:27
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>

using namespace std;

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

int cost[105][105];

int main() {
    int n;
    fin >> n;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            fin >> cost[i][j];
            if (cost[i][j] == 0 and i != j) {
                cost[i][j] = 1000000000;
            }
        }
    }

    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (cost[i][j] > cost[i][k] + cost[k][j]) {
                    cost[i][j] = cost[i][k] + cost[k][j];
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (cost[i][j] != 1000000000) {
                fout << cost[i][j] << ' ';
            } else { fout << 0 << " "; }
        }
        fout << '\n';
    }
}