Cod sursa(job #2989164)

Utilizator sdragosSandu Dragos sdragos Data 6 martie 2023 08:23:00
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
using namespace std;

int n, mat[110][110];

int main()
{
    freopen("royfloyd.in", "r", stdin);
    freopen("royfloyd.out", "w", stdout);

    cin >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            cin >> mat[i][j];

    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] && mat[k][j] && i != j && (mat[i][k] + mat[k][j] < mat[i][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++)
            cout << mat[i][j] << ' ';
        cout << '\n';
    }

    return 0;
}