Cod sursa(job #399541)

Utilizator ssergiussSergiu-Ioan Ungur ssergiuss Data 20 februarie 2010 17:26:06
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 2.27 kb
#include <algorithm>
#include <vector>
using namespace std;

#define DIM 50001
#define INF 0x3f3f3f3f

int N, M;
int cnt, cst[ DIM ], poz[ DIM ], heap[ DIM ];
vector < pair <int, int> > v[ DIM ];

inline void heap_up( const int &nod ) {

    int aux;

    while( nod > 1 ) {

        aux = nod>>1;
        if( cst[ heap[ aux ] ] > cst[ heap[ nod ] ] ) {

            swap( heap[ aux ], heap[ nod ] );
            poz[ heap[ aux ] ] = aux;
            poz[ heap[ nod ] ] = nod;
        }
        else
            return;
    }
}

inline void heap_down( const int &nod ) {

    int aux;

    while( nod<<1 <= cnt ) {

        aux = nod<<1;
        if( aux+1 <= cnt && cst[ heap[ aux+1 ] ] > cst[ heap[ aux ] ] )
            ++aux;
        if( cst[ heap[ aux ] ] > cst[ heap[ nod ] ] ) {

            swap( heap[ aux ], heap[ nod ] );
            poz[ heap[ aux ] ] = aux;
            poz[ heap[ nod ] ] = nod;
        }
        else
            return;
    }
}

inline void heap_push( const int &ind ) {

    heap[ ++cnt ] = ind;
    poz[ ind ] = cnt;
    heap_up( cnt );
}

inline void heap_pop() {

    heap[ 1 ] = heap[ cnt-- ];
    poz[ heap[ 1 ] ] = 1;
    heap_down( 1 );
}

int main() {

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

    int i, x, y, c, ind;
    vector < pair <int, int> > :: iterator it;

    scanf( "%d %d", &N, &M );
    for( i = 0; i < M; ++i ) {

        scanf( "%d %d %d", &x, &y, &c );
        v[ x ].push_back( make_pair( y, c ) );
    }

    for( i = 1; i <= N; ++i )
        cst[ i ] = INF;
    ++cnt;
    heap_push( 1 );
    cst[ 1 ] = 0;
    poz[ 1 ] = 1;
    while( cnt ) {

        ind = heap[ 1 ];
        heap_pop();
        for( it = v[ ind ].begin(); it != v[ ind ].end(); ++it )
            if( cst[ ind ] + it->second < cst[ it->first ] ) {

                cst[ it->first ] = cst[ ind ] + it->second;
                if( poz[ it->first ] )
                    heap_up( poz[ it->first ] );
                else
                    heap_push( it->first );
            }
    }

    for( i = 2; i <= N; ++i )
        if( cst[ i ] == INF )
            printf( "0 " );
        else
            printf( "%d ", cst[ i ] );

    return 0;
}