Cod sursa(job #3215727)

Utilizator victor_gabrielVictor Tene victor_gabriel Data 15 martie 2024 12:13:32
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>
#include <climits>

using namespace std;

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

const int DIM = 110;

int n, x;
int mat[DIM][DIM];

void initMatrix() {
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            mat[i][j] = INT_MAX;
}

int main() {
    fin >> n;

    initMatrix();
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            fin >> x;
            if (x != 0)
                mat[i][j] = x;
        }
    }

    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (mat[i][k] == INT_MAX || mat[k][j] == INT_MAX || i == j)
                    continue;
                if (mat[i][j] > mat[i][k] + mat[k][j])
                    mat[i][j] = mat[i][k] + mat[k][j];
            }
        }
    }

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

    return 0;
}