Cod sursa(job #1053845)

Utilizator ioalexno1Alexandru Bunget ioalexno1 Data 12 decembrie 2013 23:22:14
Problema Sate Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 0.96 kb
#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;
}