Cod sursa(job #3377251)

Utilizator Alexutu008Ionita Alexandru-Dumitru Alexutu008 Data 22 noiembrie 2026 15:31:43
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>

using namespace std;

int n, a[101][101];

void rf()
{
    for (int k = 1; k <= n; ++k)
    {
        for (int i = 1; i <= n; ++i)
        {
            for (int j = 1; j <= n; ++j)
            {
                if (a[i][k] != 0 && a[k][j] != 0 && (a[i][j] > a[i][k] + a[k][j] || a[i][j] == 0) && i != j)
                    a[i][j] = a[i][k] + a[k][j];
            }
        }
    }
}

int main()
{
    freopen("royfloyd.in", "r", stdin);
    freopen("royfloyd.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);

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

    rf();

    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= n; ++j)
        {
            cout << a[i][j] << ' ';
        }
        cout << '\n';
    }

    return 0;
}