Cod sursa(job #2723278)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 13 martie 2021 20:26:49
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("royfloyd.in");
ofstream fout ("royfloyd.out");

void usain_bolt()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
}

const int N = 1e2 + 5;

int a[N][N];

bool voids(int x, int y, int add)
{
    if(add == x) {
        return true;
    }
    if(add == y) {
        return true;
    }
    if(x == y) {
        return true;
    }
    if(a[x][add] == 0) {
        return true;
    }
    if(a[add][y] == 0) {
        return true;
    }
    return false;
}

int main()
{
    usain_bolt();

    int n;

    fin >> n;
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= n; ++j) {
            fin >> a[i][j];
        }
    }
    for(int k = 1; k <= n; ++k) {
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= n; ++j) {
                if(voids(i, j, k)) {
                    continue;
                }
                a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
            }
        }
    }
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= n; ++j) {
            fout << a[i][j] << " ";
        }
        fout << "\n";
    }
    return 0;
}