Cod sursa(job #2667807)

Utilizator ReksioCroftOctavian Florin Staicu ReksioCroft Data 3 noiembrie 2020 21:14:32
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <cstdio>
#include <cassert>
#include <vector>
#include <queue>
#include <cmath>

using namespace std;
priority_queue< pair< int, int>> q;
const int nMax = 30001;
int d[nMax];
vector< pair< int, int>> v[nMax];

void calcDist( int nod ) {
    for ( pair< int, int > &i:v[ nod ] ) {
        if ( i.first > nod )
            q.push( { -i.second, i.first } );
        else
            q.push( { i.second, i.first } );
    }
    while ( !q.empty() ) {
        int nodC, dist;
        nodC = q.top().second;
        dist = -q.top().first;
        q.pop();
        if ( d[ nodC ] == 0 || d[ nodC ] > dist ) {
            d[ nodC ] = dist;
            for ( pair< int, int > i : v[ nodC ] ) {
                if ( i.first > nodC )
                    q.push( { -( dist + i.second ), i.first } );
                else
                    q.push( { -( dist - i.second ), i.first } );
            }
        }
    }
}

int main() {
    int n, m, x, y;
    FILE *fin, *fout;
    fin = fopen( "sate.in", "r" );
    assert( fscanf( fin, "%d%d%d%d", &n, &m, &x, &y ) == 4 );
    for ( int i = 0; i < m; i++ ) {
        int a, b, c;
        assert( fscanf( fin, "%d%d%d", &a, &b, &c ) == 3 );
        v[ a ].push_back( { b, c } );
        v[ b ].push_back( { a, c } );
    }
    fclose( fin );

    calcDist( x );

    fout = fopen( "sate.out", "w" );
    fprintf( fout, "%d\n", abs( d[ y ] ) );
    fclose( fout );
    return 0;
}