Cod sursa(job #2926121)

Utilizator UnknownPercentageBuca Mihnea-Vicentiu UnknownPercentage Data 16 octombrie 2022 23:31:59
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.79 kb
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define vt vector
#define pb push_back
#define em emplace
#define emb emplace_back

#define all(x) x.begin(), x.end()
#define all1(x) x.begin() + 1, x.end()
#define sz(x) (int)(x).size()

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template <class T> void re(vt <T>& x);
template <class T> void re(T& x) {
    cin >> x;
}

template <class H, class... T> void re(H &x, T&... y) {
    re(x); re(y...);
}

template <class T> void re(vt <T>& x) {
    for(auto& it : x)
        re(it);
}

template <class T> void wr(T x) {
    cout << x;
}

template <class H, class ...T> void wr(H x, T... y) {
    wr(x); wr(y...);
}

inline void Open(const string Name) {
    #ifndef ONLINE_JUDGE
        (void)!freopen((Name + ".in").c_str(), "r", stdin);
        (void)!freopen((Name + ".out").c_str(), "w", stdout);
    #endif
}

void solve() {
    int n; re(n);
    const int INF = 1e9 + 7;
    vt <vt <int>> a(n, vt <int>(n)); re(a);

    for(int i = 0;i < n;i++)
        for(int j = 0;j < n;j++)
            if(a[i][j] == 0) {
                a[i][j] = INF;
            }
    for(int k = 0;k < n;k++)
        for(int i = 0;i < n;i++)
            for(int j = 0;j < n;j++) {
                if(i != j && a[i][k] < INF && a[k][j] < INF) {
                    a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
                }
            }

    for(int i = 0;i < n;i++, wr('\n'))
        for(int j = 0;j < n;j++) {
            if(a[i][j] == INF) a[i][j] = 0;
            wr(a[i][j], ' ');
        }
}

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

    //Open("");

    int t = 1;
    for(;t;t--) {
        solve();
    }

    return 0;
}