Cod sursa(job #358957)

Utilizator GheorgheMihaiMihai Gheorghe GheorgheMihai Data 25 octombrie 2009 11:22:54
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include<stdio.h>
#include<vector>
using namespace std;
vector <short int> a[30002];
vector <int> c[30002];
int d[30002];
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",&n,&m,&x,&Y);
	int i,cost,j;
	char s[32];
	short int cx,cy;
	for(i=1;i<=m;i++)
	{
		gets(s);
		cx=0;cy=0;cost=0;
		for(j=0;s[j]!=' ';j++)
			cx=cx*10+s[j]-'0';
		for(++j;s[j]!=' ';j++)
			cy=cy*10+s[j]-'0';
		for(++j;s[j];j++)
			cost=cost*10+s[j]-'0';
//		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;
}