Pagini recente » Cod sursa (job #2708078) | Cod sursa (job #2350625) | Cod sursa (job #436268) | Cod sursa (job #187507) | Cod sursa (job #308306)
Cod sursa(job #308306)
#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;
}