Cod sursa(job #1217754)

Utilizator AndreiBarbutaAndrei Barbuta AndreiBarbuta Data 8 august 2014 02:21:28
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <fstream>
#include <vector>
#include <queue>

#define IN "sate.in"
#define OUT "sate.out"
#define pb push_back
#define p push


const int MAX = 30014 ;

using namespace std;

ifstream fin ( IN ) ;
ofstream fout ( OUT ) ;

struct sat{
    int casa , bani ;
};

inline sat build ( int a , int b ){
    sat aux ;
    aux.casa = a ;
    aux.bani = b ;
    return aux ;
}

vector < sat > gr [ MAX ] ;
queue < int > q ;

int d [ MAX ] ;
bool fol [ MAX ] ;

void bfs ( int nod  ) ;

int main()
{
    int n , m , x , y , start , finish , cost;
    fin >> n >> m >> start >> finish ;
    for ( register int i = 1 ;i <= m ; ++ i){
        fin >> x >> y >> cost ;
        gr [ x ].pb ( build( y , cost ) ) ;
        gr [ y ].pb ( build( x , - cost ) ) ;
    }
    bfs ( start ) ;
    fout << d [ finish ] ;
    return 0;
}

void bfs ( int nod ){
    q.p ( nod ) ;
    fol [ nod ] = 1 ;
    while ( ! q.empty ( ) ){
        int x = q.front ( ) ;
        q.pop ( ) ;
        for ( register int i = 0 ; i < gr [ x ].size ( ) ; ++ i )
            if ( ! fol [ gr [ x ] [ i ].casa ] ){
                q.p ( gr [ x ] [ i ].casa ) ;
                fol [ gr [ x ] [ i ].casa ] = 1 ;
                d [ gr [ x ] [ i ].casa ] = d [ x ] + gr [ x ] [ i ].bani ;
            }
    }

}