Cod sursa(job #3332291)

Utilizator andreitricaAndrei Trica andreitrica Data 5 ianuarie 2026 21:47:53
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>

using namespace std;

const int nmax=105;
const int inf=1e9;
int dp[nmax][nmax];
int n;

//#define a dp

void citire(){
    cin>>n;
    for (int i=1; i<=n; ++i){
        for (int j=1; j<=n; ++j){
            cin>>dp[i][j];
            if(!dp[i][j] && i!=j)
                dp[i][j]=inf;
        }
    }
}

void royfloyd(){
    int i, j, k;

    for (k=1; k<=n; ++k){
        for(i=1; i<=n; ++i){
            for(j=1; j<=n; ++j){
                dp[i][j]=min(dp[i][j], dp[i][k]+dp[k][j]);
            }
        }
    }
}

void afisare(){

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

}

int main(){

    freopen ("royfloyd.in", "r", stdin);
    freopen ("royfloyd.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    citire();

    royfloyd();

    afisare();
    return 0;
}