Cod sursa(job #400886)

Utilizator ssergiussSergiu-Ioan Ungur ssergiuss Data 22 februarie 2010 09:21:54
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include <algorithm>
using namespace std;

#define DIM 1<<7
#define INF 0x3f3f3f3f
#define MAX 1<<14

char s[ MAX ];
int N;
int cnt, cst[ DIM ][ DIM ];

inline void roy_floyd() {

    int i, j, k;

    for( k = 1; k <= N; ++k )
        for( i = 1; i <= N; ++i )
            for( j = 1; j <= N; ++j )
                if( cst[ i ][ k ] && cst[ k ][ i ] && cst[ i ][ k ] + cst[ k ][ j ] < cst[ i ][ j ] )
                    cst[ i ][ j ] = cst[ i ][ k ] + cst[ k ][ j ];
}

inline void read( int &val ) {

    while( !isdigit( s[ cnt ] ) )
        if( ++cnt == MAX ) {

            fread( s, 1, MAX, stdin );
            cnt = 0;
        }
    val = 0;
    while( isdigit( s[ cnt ] ) ) {

        val = val*10 + s[ cnt ] - '0';
        if( ++cnt == MAX ) {

            fread( s, 1, MAX, stdin );
            cnt = 0;
        }
    }
}

int main() {

    freopen( "royfloyd.in", "r", stdin );
    freopen( "royfloyd.out", "w", stdout );

    int i, j;

    read( N );
    for( i = 1; i <= N; ++i )
        for( j = 1; j <= N; ++j ) {

            read( cst[ i ][ j ] );
            if( i != j && !cst[ i ][ j ] )
                cst[ i ][ j ] = INF;
        }

    roy_floyd();

    for( i = 1; i <= N; ++i ) {

        for( j = 1; j <= N; ++j )
            if( cst[ i ][ j ] == INF )
                printf( "0 " );
            else
                printf( "%d ", cst[ i ][ j ] );
        printf( "\n" );
    }

    return 0;
}