Cod sursa(job #323080)

Utilizator GheorgheMihaiMihai Gheorghe GheorgheMihai Data 10 iunie 2009 17:35:53
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include<cstdio>
#include<vector>
using namespace std;
vector <int> q(30002);
vector <int> a[30002];
vector <int> c[30002];
vector <int> d(30002,0);
int n,m,x,Y;

void read()
{
	freopen("sate.in","r",stdin);
	freopen("sate.out","w",stdout);
	scanf("%d%d%d%d",&n,&m,&x,&Y);
	int i,cost,cx,cy;
	for(i=1;i<=m;i++)
	{
		scanf("%d%d%d",&cx,&cy,&cost);
		a[cx].push_back(cy);
		a[cy].push_back(cx);
		c[cx].push_back(cost);
		c[cy].push_back(cost);
	}
}

void rez()
{
	int i,y,cost,p=1,u=0;
	q[++u]=x;
	while(d[Y]==0)
	{
		x=q[p++];
		for(i=0;i<a[x].size();i++)
		{
			y=a[x][i];
			if(d[y]==0)
			{
				cost=c[x][i];
				d[y]=(x<y ? d[x]+cost : d[x]-cost);
				q[++u]=y;
			}
		}
	}
	printf("%d\n",d[Y]);
}

int main()
{
	read();
	rez();
	return 0;
}