Cod sursa(job #3217917)

Utilizator TeodoraMaria123Serban Teodora Maria TeodoraMaria123 Data 25 martie 2024 10:10:22
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <bits/stdc++.h>

using namespace std;

const int INF = 1e9;

int n;
vector <vector <int> > d;

void royfloyd()
{
    for(int k = 1; k <= n; k ++)
    {
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++)
            {
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
    }

}

int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);

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

    cin >> n;
    d.resize(n + 1, vector <int> (n + 1));

    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= n; j ++)
        {
            cin >> d[i][j];
            if(d[i][j] == 0)
                d[i][j] = INF;
        }
    }

    royfloyd();

    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= n; j ++)
            if(i == j  || d[i][j] == INF)
                cout << "0 ";
            else
                cout << d[i][j] << " ";
        cout << "\n";
    }
    return 0;
}