Cod sursa(job #867937)

Utilizator marius135Dumitran Adrian Marius marius135 Data 30 ianuarie 2013 14:09:28
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include<stdio.h>
#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,i;
    freopen("sate.in","r",stdin);
    freopen("sate.out","w",stdout);
    scanf("%ld %ld %ld %ld",&n,&m,&x,&Y);
    for(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));
}