Cod sursa(job #308306)

Utilizator DraStiKDragos Oprica DraStiK Data 26 aprilie 2009 18:35:30
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <stdio.h>
#define DIM 30005
using namespace std;
struct nod {int x,d;
            nod *urm;} *lst[DIM];
int c[DIM],cost[DIM];
int n,m,a,b;
void add (int x,int y,int d)
{
    nod *p=new nod;
    p->x=y;
    p->d=d;
    p->urm=lst[x];
    lst[x]=p;
}
void read ()
{
    int i,x,y,d;
    scanf ("%d%d%d%d",&n,&m,&a,&b);
    for (i=1; i<=m; ++i)
    {
        scanf ("%d%d%d",&x,&y,&d);
        add (x,y,d);
        add (y,x,d);
    }
}
void bfs (int start)
{
    nod *p;
    int in,sf;
    for (c[in=sf=1]=start; in<=sf; ++in)
        for (p=lst[c[in]]; p; p=p->urm)
            if (!cost[p->x])
            {
                if (c[in]<p->x)
                    cost[p->x]=cost[c[in]]+p->d;
                else
                    cost[p->x]=cost[c[in]]-p->d;
                if (p->x==b)
                    return ;
                c[++sf]=p->x;
            }
}
int main ()
{
    freopen ("sate.in","r",stdin);
    freopen ("sate.out","w",stdout);
    read ();
    bfs (a);
    printf ("%d",cost[b]);
    return 0;
}