Mai intai trebuie sa te autentifici.

Cod sursa(job #782269)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 26 august 2012 16:25:27
Problema Sate Scor 100
Compilator cpp Status done
Runda #1 Marime 1 kb
#include<stdio.h>
struct sate
{
    long l[263000],deque[263000],f[65500];
};
sate v;
long ap[65500],x,y,val[263000],p;
long dfs(long a,long b)
{
    long x1,y1,sum;
    if (a==y)
        return b;
    ap[a]=1;
    x1=v.f[a];
    while(x1)
    {
        y1=v.deque[x1];
        if(ap[y1])
        {
            x1=v.l[x1];
            continue;
        }
        sum=val[(x1+1)/2];
        if(y1<a)
            p=dfs(y1,b-sum);
        else
            p=dfs(y1,b+sum);
        if(p)
            return p;
        x1=v.l[x1];
    }
    return 0;
}
int main()
{
    long n,m,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.deque[i*2-1],&v.deque[i*2],&val[i]);
        v.l[i*2]=v.f[v.deque[i*2-1]] ;
        v.l[i*2-1]=v.f[v.deque[i*2]] ;
        v.f[v.deque[i*2-1]]=i*2;
        v.f[v.deque[i*2]]=i*2-1 ;
    }
    printf("%ld\n",dfs(x,0));
    return 0;
}