Cod sursa(job #2844078)

Utilizator OvidRata Ovidiu Ovid Data 3 februarie 2022 18:28:59
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll


int t, n, m, k, a[300010], q, l, r;

int mat[101][101];


ifstream fin("royfloyd.in"); ofstream fout("royfloyd.out");
#define cin fin
#define cout fout


int32_t main(){
INIT
cin>>n;
for(int i=1; i<=n; i++){
    for(int j=1; j<=n; j++){
        cin>>mat[i][j];
    }
}


for(int k=1; k<=n; k++){
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            if( (mat[i][k]>0) && (mat[k][j]>0) && (i!=j) ){
                if(mat[i][j]==0){
                    mat[i][j]=mat[i][k]+mat[k][j];
                }
                else{
                    mat[i][j]=min(mat[i][j], mat[i][k]+mat[k][j]);
                }
            }
        }
    }
}



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


return 0;
}