Cod sursa(job #1815681)

Utilizator jurjstyleJurj Andrei jurjstyle Data 25 noiembrie 2016 17:32:09
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std ;

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

int n , m , x , y , checking_order = 1 ;
vector < pair <int,int> > G[30002] ;
vector <int> d ;
vector <bool> V ;
queue <int> Q ;


void BFS ( int start_node )
{
    d[start_node] = 0 ;
    V[start_node] = 1 ;
    Q.push ( start_node ) ;

    while ( !Q.empty () )
    {
        int current_node = Q.front () ;
        for ( const pair<int,int> & y : G[current_node] )
        {
            if ( V[y.first] ) continue ;
            d[y.first] = d[current_node] + checking_order * y.second ;
            V[y.first] = 1 ;
            Q.push ( y.first ) ;
        }
        Q.pop() ;
    }
}


int main ()
{
    f >> n >> m >> x >> y ;
    for ( int cnt = 1 ; cnt <= m ; ++cnt )
    {
        int i , j , d ;
        f >> i >> j >> d ;
        G[i].push_back ( make_pair ( j , d ) ) ;
        G[j].push_back ( make_pair ( i , -d ) ) ;
    }
    if ( x == y )
    {
        g << '0' ;
        return 0 ;
    }
    if ( y < x )
        checking_order *= -1 ;
    V.resize ( n + 1 ) ;
    d.resize ( n + 1 ) ;

    BFS ( x ) ;

    g << d[y] ;


    f.close() ;
    g.close() ;
    return 0 ;
}