Cod sursa(job #333869)

Utilizator IoannaPandele Ioana Ioanna Data 24 iulie 2009 13:46:53
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include<stdio.h>
#define nmax 30010
long n,m,x,y;
long v[nmax];
struct queue
{
long v,c;
};

queue q[nmax];

struct nod
{
nod *urm;
int v,c;
};

nod *t[nmax];

void InsertBegin(long a,long b,long c)
{
nod *aux;
aux=new nod;
aux->v=b;
aux->c=c;
aux->urm=t[a];
t[a]=aux;
aux=new nod;
aux->v=a;
aux->c=c;
aux->urm=t[b];
t[b]=aux;
}



void read()
{
scanf("%ld%ld%ld%ld",&n,&m,&x,&y);
long i;
long a,b,c;
for (i=1;i<=m;i++)
    {
     scanf("%ld%ld%ld",&a,&b,&c);
     InsertBegin(a,b,c);
    }
}

void lee()
{
long st,dr;
nod *p;
st=dr=1;
q[st].v=x;
v[x]=1;
while (st<=dr)
      {
       if (q[st].v==y)
          {
           printf("%ld",q[st].c);
           break;
          }
       for (p=t[q[st].v];p;p=p->urm)
           {
            if (!v[p->v])
               {
                q[++dr].v=p->v;
                if (p->v>q[st].v)
                    q[dr].c=q[st].c+p->c;
                else q[dr].c=q[st].c-p->c;
                v[p->v]=1;
               }
           }
       st++;
      }
}

int main()
{
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
read();
lee();
return 0;
}