Cod sursa(job #3345113)

Utilizator bogdannn_Goian Bogdan bogdannn_ Data 7 martie 2026 23:06:57
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf=1e18;
int main(){
    int n; cin>>n;
    vector<vector<ll>> dist(n+1, vector<ll>(n+1, inf));
    for(int i=1; i<=n; i++) dist[i][i]=0;
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            int x; cin>>x; if(x>0) dist[i][j]=x;
        }
    }

    for(int k=1; k<=n; k++){
        for(int i=1; i<=n; i++){
            for(int j=1; j<=n; j++)
                if(dist[i][j]>dist[i][k]+dist[k][j])
                    dist[i][j]=dist[i][k]+dist[k][j];
        }
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            if(dist[i][j]==inf) cout<<"0 ";
            else cout<<dist[i][j]<<" ";
        }
        cout<<'\n';
    }

}