Cod sursa(job #757603)

Utilizator matei_cChristescu Matei matei_c Data 12 iunie 2012 18:46:36
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include<cstdio>
#define maxm 1024*128*2
#define maxn 32*1024

long next[maxm],v[maxm],first[maxn],sel[maxn],Y,t[maxm] ;

long df(long a,long b)
{
	
	long x,y ;
	
	if( a == Y )
		return b ;
	
	sel[a] = 1 ;
	x = first[a] ;
	
	while(x)
	{
		
		y = v[x] ;
		if( sel[y] )
		{
			x = next[x];
			continue;
		}
		long val = t[(x+1)/2] , p ;
		
		if( y < a)
			p = df( y , b-val ) ; 
		else
			p = df( y , b + val ) ;
		if( p )
			return p ;
		x = next[x] ;
		
	}
	
	return 0;
	
}

int main()
{
	
	long n,m,x;
	
	freopen("sate.in","r",stdin);
	freopen("sate.out","w",stdout);
	
	scanf("%ld%ld%ld%ld",&n,&m,&x,&Y);
	for(int i=1;i<=m;++i)
	{
		
		scanf("%ld%ld%ld",&v[i*2-1],&v[i*2],&t[i]);
		
		long a = v[i*2-1] , b = v[i*2] ;
		
		next[i*2] = first[a] ;
		next[i*2-1] = first[b] ;
		
		first[a] = i * 2 ;
		first[b] = i * 2 - 1 ;
		
	}
	
	printf("%ld\n",df(x,0));
	
	return 0 ;
	
}