Cod sursa(job #3241441)

Utilizator CondoracheAlexandruCondorache Alexandru CondoracheAlexandru Data 30 august 2024 13:08:50
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define F first
#define S second
#define endl '\n'
#define all(a) (a).begin(),(a).end()
using namespace std;
const int maxn = 105;
#define fin cin
#define fout cout
//ifstream fin("royfloyd.in");
//ofstream fout("royfloyd.out");

ll min(ll a, ll b) {
    if (a < b) return a;
    return b;
}

void floyd_warshall(ll a[maxn][maxn], int n) {
    for (int k = 0; k < n; k++) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (a[i][k] && a[k][j] && (a[i][j] > a[i][k] + a[k][j] || !a[i][j]) && i != j) {
                    a[i][j] = a[i][k] + a[k][j];
                }
            }
        }
    }
}



void solve() {
    int n;
    fin >> n;
    ll a[maxn][maxn] = {0};
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            fin >> a[i][j];
        }
    }
    floyd_warshall(a, n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
                fout << a[i][j] << " ";
        }
        fout << endl;
    }
}
 
int main() {
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}