Cod sursa(job #628789)

Utilizator SmarandaMaria Pandele Smaranda Data 2 noiembrie 2011 09:52:37
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include<cstdio>
#include<queue>
#include<vector>
struct ORAS
{
	long w,d;
};
using namespace std;
int main () {
	
	long n,m,x,y,i,a,b,dist;
	vector <ORAS> t[30004];
	vector <ORAS> :: iterator it;
	queue<ORAS> q;
	ORAS temp,temp2;
	
	freopen("sate.in","r",stdin);
	freopen("sate.out","w",stdout);
	
	scanf("%ld%ld%ld%ld",&n,&m,&x,&y);
	for (i=1;i<=m;i++)
	{
		scanf("%ld%ld%ld",&a,&b,&dist);
		temp.w=b;
		temp.d=dist;
		t[a].push_back(temp);
		temp.w=a;
		t[b].push_back(temp);
	}
	temp.w=x;
	temp.d=0;
	q.push(temp);
	while (!q.empty()) {
		temp=q.front();
		for (it=t[temp.w].end()-1;it>=t[temp.w].begin();--it) {
			temp2.w=(*it).w;
			temp2.d=(*it).d;
			if (temp.w>temp2.w)
				temp2.d=temp.d-temp2.d;
			else
				temp2.d=temp.d+temp2.d;
			if (temp2.w==y)
			{
				printf("%ld\n",temp2.d);
				break;
			}
			t[temp.w].pop_back();
			q.push(temp2);
		}
		q.pop();
	}
	return 0;
}