Cod sursa(job #3325404)

Utilizator eddy_cimpanuCimpanu Eduardo Daniel eddy_cimpanu Data 25 noiembrie 2025 13:26:11
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <iostream>
#include <fstream>
using namespace std;

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

    int n;
    fin >> n;

    int chestie[120][120];

    for (int a = 1; a <= n; a++) {
        for (int b = 1; b <= n; b++) {
            fin >> chestie[a][b];
            if (a != b && chestie[a][b] == 0) chestie[a][b] = 999999999;
        }
    }

    for (int x = 1; x <= n; x++) {
        for (int y = 1; y <= n; y++) {
            for (int z = 1; z <= n; z++) {
                int t1 = chestie[y][x];
                int t2 = chestie[x][z];
                if (t1 != 999999999 && t2 != 999999999) {
                    int t3 = t1 + t2;
                    if (t3 < chestie[y][z]) chestie[y][z] = t3;
                }
            }
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            fout << (chestie[i][j] == 999999999 ? 0 : chestie[i][j]) << " ";
        }
        fout << "\n";
    }

    return 0;
}