Cod sursa(job #350091)

Utilizator GheorgheMihaiMihai Gheorghe GheorgheMihai Data 22 septembrie 2009 18:01:30
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include<stdio.h>
short int a[1<<15][1<<15];
int c[1<<15][1<<15];
int d[1<<15];
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][++a[cx][0]]=cy;
		a[cy][++a[cy][0]]=cx;
		c[cx][++c[cx][0]]=cost;
		c[cy][++c[cy][0]]=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][0];
		for(i=1;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;
}