Cod sursa(job #3231309)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 25 mai 2024 17:11:00
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f ("royfloyd.in");
ofstream g ("royfloyd.out");

const int NMAX = 1e2;

int a[NMAX+1][NMAX+1], dp[NMAX+1][NMAX+1];

int main()
{
    
    int n;
    f >> n;
    
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++){
            
            f >> a[i][j];
            
            if(i != j and a[i][j] == 0)
                a[i][j] = 1e9;
            
            dp[i][j] = a[i][j];
            
        }

    for(int k=1; k<=n; k++)
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
                
                
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++)
            if(dp[i][j] == 0)
                g << 0 << ' ';
            else g << dp[i][j] << ' '; 
            
        g << "\n";
    }

    return 0;
}