Cod sursa(job #3350476)

Utilizator waiaselNechifor waiasel Data 8 aprilie 2026 17:30:59
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.24 kb
#include <bits/stdc++.h>
#include <fstream>
using namespace std;

const int inf = 1000000000;
int mat[505][505];

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

    int n;
    fin >> n;
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= n; ++j) {
            int aux;
            fin >> aux;
            if(i == j) {
                mat[i][j] = 0;
            }
            else if(aux == 0) {
                mat[i][j] = inf;
            }
            else {
                mat[i][j] = aux;
            }
        }
    }

    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] != inf && mat[k][j] != inf) {
                    if(mat[i][k] + mat[k][j] < mat[i][j]) {
                        mat[i][j] = mat[i][k] + mat[k][j];
                    }
                }
            }
        }
    }

    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= n; ++j) {
            if(mat[i][j] == inf) {
                fout << 0 << " ";
            }
            else {
                fout << mat[i][j] << " ";
            }
        }
        fout << '\n';
    }
    return 0;
}