Cod sursa(job #2749332)

Utilizator Vlad_PipereaPiperea Vlad Vlad_Piperea Data 6 mai 2021 14:44:52
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>

using namespace std;

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

const int N = 105 , INF = 1005 ;
int a[N][N] , b[N][N] , n ;

int main()
{
    in >> n ;
    for ( int i = 1 ; i <= n ; i++ )
    {
        for ( int j = 1 ; j <= n ; j++ )
        {
            in >> a[i][j] ;
            if ( i == j )
            {
                b[i][j] = 0 ;
            }
            else if ( a[i][j] == 0 )
            {
                b[i][j] = INF ;
            }
            else b[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++ )
            {
                b[i][j] = min(b[i][j] , b[i][k] + b[k][j]) ;
            }
        }
    }
    for ( int i = 1 ; i <= n ; i++ )
    {
        for ( int j = 1 ; j <= n ; j++ )
        {
            out << b[i][j] << " ";
        }
        out << '\n' ;
    }
    in.close() ;
    out.close() ;
    return 0;
}