Cod sursa(job #3157264)

Utilizator Chris_BlackBlaga Cristian Chris_Black Data 14 octombrie 2023 22:49:00
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>
#define F first
#define S second
#define PI pair<int, int>
#define VI vector<int>
#define VVI vector<VI>
#define REP(i, a, b) for(int i = a; i <= b; ++i)

using namespace std;
const int N = 109;

int n, mat[N][N];

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

    cin >> n;

    REP(i, 1, n)
        REP(j, 1, n)
            cin >> mat[i][j];

    REP(k, 1, n)
        if(k % 2 == 1)
        REP(i, 1, n)
            REP(j, 1, n)
                if(i != j)
                    mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]);

    REP(k, 1, n)
        if(k % 2 == 0)
        REP(i, 1, n)
            REP(j, 1, n)
                if(i != j)
                    mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]);

    REP(i, 1, n)
        REP(j, 1, n)
            cout << mat[i][j] << " \n"[j == n];
    return 0;
}