Cod sursa(job #350088)

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

void read()
{
	freopen("sate.in","r",stdin);
	freopen("sate.out","w",stdout);
	scanf("%d%d%hd%hd",&n,&m,&x,&Y);
	int i,cost;
	short int cx,cy;
	for(i=1;i<=m;i++)
	{
		scanf("%hd%hd%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,cost,p=1,u=0,lim;
	short int y;
	q[++u]=x;
	while(d[Y]==0)
	{
		x=q[p++];
		lim=a[x].size();
		for(i=0;i<lim;i++)
		{
			y=a[x][i];
			if(d[y]==0)
			{
				cost=c[x][i];
				if(x<y)
					d[y]=d[x]+cost;
				else
					d[y]=d[x]-cost;
				q[++u]=y;
			}
		}
	}
	printf("%d\n",d[Y]);
}

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