Pagini recente » Cod sursa (job #276827) | Cod sursa (job #1510052) | Cod sursa (job #889059) | Cod sursa (job #1357039) | Cod sursa (job #1053845)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int MAX_N = 3 * 1e5 + 1;
vector < pair< int, int > > G[MAX_N];
queue <int> Q;
bool viz[MAX_N];
int N, X, Y, sol[MAX_N];
void read(){
ifstream cin( "sate.in" );
int M;
cin >> N >> M >> X >> Y;
for(; M >= 1; -- M ){
int x, y, d;
cin >> x >> y >> d;
G[x].push_back( make_pair( y, d ) ) ;
G[y].push_back( make_pair( x, -d ) ) ;
}
cin.close();
}
int bfs(){
viz[X] = 1;
Q.push( X );
while( !Q.empty() ){
int curr = Q.front();
for( vector < pair< int, int > >::iterator it = G[curr].begin(); it != G[curr].end(); ++it )
if( !viz[ (*it).first ] ){
viz[ (*it).first ] = 1;
Q.push( (*it).first );
sol[ (*it).first ] = sol[curr] + (*it).second;
if( (*it).first == Y ) return sol[Y];
}
Q.pop();
}
}
int main(){
read();
ofstream cout( "sate.out" );
cout << bfs();
cout.close();
return 0;
}