Cod sursa(job #3217858)

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

using namespace std;

const int NMAX = 256;

int n;
int a[NMAX + 5][NMAX + 5], d[NMAX + 5][NMAX + 5];

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

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

    cin >> n;

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

    for(int k = 1; k <= n; k ++)
    {
        for(int j = 1; j <= n; j ++)
        {
            for(int i = 1; i <= n; i ++)
            {
                if(a[i][k] + a[k][j] < a[i][j])
                {
                    a[i][j] = a[i][k] + a[k][j];
                    d[i][j] = d[i][k] + d[k][j];
                }
                else if(a[i][k] + a[k][j] == a[i][j]  &&  d[i][k] + d[k][j] > d[i][j])
                    d[i][j] = d[i][k] + d[k][j];
            }
        }
    }

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

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