Cod sursa(job #521252)

Utilizator klamathixMihai Calancea klamathix Data 11 ianuarie 2011 20:59:04
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;

const int maxn = 35000;
bool seen[maxn];
queue <int> Q;
vector <pair <int , int > > G[maxn];
int i , j , n , m , x , y, a , b , c;
int d[maxn];

void BF()
{
	int i;
	Q.push(x);
	
	for( ; !Q.empty() ; ) {
		int act = Q.front();
		Q.pop();
		seen[act] = true;
		if ( act == y ) return;
		for( i = 0 ; i < G[act].size() ; ++i ) 
			if ( ! seen[G[act][i].first] ) {
				Q.push(G[act][i].first);
				d[G[act][i].first] = d[act] + G[act][i].second;
			}
	}

}

int main()
{
	freopen("sate.in","r",stdin);
	freopen("sate.out","w",stdout);
	
	scanf("%d %d %d %d",&n,&m,&x,&y);
	
	if ( x > y ) swap(x,y);
	
	for( i = 1 ; i <= m ; ++i ) {
		scanf("%d %d %d",&a,&b,&c);
		if ( a > b ) swap(a,b);
		G[a].push_back(make_pair(b,c));
		G[b].push_back(make_pair(a,-c));
	}
	
	BF();
	
	printf("%d\n",d[y]);
	
return 0;
}