Pagini recente » Cod sursa (job #1812538) | Cod sursa (job #2422285) | Cod sursa (job #1875252) | Cod sursa (job #2390151) | Cod sursa (job #1702529)
/**
* Code by Patrick Sava
* "Spiru Haret" National College of Bucharest
**/
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int MAX = 3e4 + 14 ;
int dist [ MAX ] ;
queue < int > Q ;
vector < pair < int , int > > gr [ MAX ] ;
ifstream cin ( "sate.in" ) ;
ofstream cout ( "sate.out" ) ;
int main()
{
int n , m , a , b ;
cin >> n >> m >> a >> b ;
while ( m -- ) {
int x , y , cost ;
cin >> x >> y >> cost ;
gr [ x ].push_back ( make_pair ( y , cost ) ) ;
gr [ y ].push_back ( make_pair ( x , -cost ) ) ;
}
for ( int i = 1 ; i <= n ; ++ i ) {
dist [ i ] = 1 << 29 ;
}
dist [ a ] = 0 ;
Q.push ( a ) ;
while ( !Q.empty() )
{
int nod = Q.front() ;
Q.pop() ;
for ( auto x : gr [ nod ] ) {
if ( dist [ x.first ] > dist [ nod ] + x.second ) {
dist [ x.first ] = dist [ nod ] + x.second ;
Q.push ( x.first ) ;
}
}
}
cout << dist [ b ] << '\n' ;
return 0;
}