Cod sursa(job #2966079)

Utilizator victor_gabrielVictor Tene victor_gabriel Data 16 ianuarie 2023 18:48:48
Problema Floyd-Warshall/Roy-Floyd Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <fstream>
#include <climits>

using namespace std;

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

const int DIM = 110;
int n;
int a[DIM][DIM];

int main() {
    fin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            fin >> a[i][j];
            if (i == j)
                a[i][j] = 0;
            if (a[i][j] == 0 && i != j)
                a[i][j] = INT_MAX;
        }
    }

    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (a[i][j] > a[i][k] + a[k][j])
                    a[i][j] = a[i][k] + a[k][j];

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == j)
                fout << 0 << ' ';
            else
                fout << (a[i][j] != INT_MAX ? a[i][j] : 0) << ' ';
        }
        fout << '\n';
    }

    return 0;
}