Pagini recente » Cod sursa (job #2004423) | Cod sursa (job #1985826) | Cod sursa (job #1750980) | Cod sursa (job #393149) | Cod sursa (job #133446)
Cod sursa(job #133446)
#include <stdio.h>
#include <stdlib.h>
int n, m, x, y, viz[30000];
typedef struct nod
{
int x, d;
nod *a;
} *pNod;
pNod v[30001];
void citire()
{
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
int i, a, b, d;
pNod p;
scanf("%d %d %d %d",&n,&m,&x,&y);
for (i = 1; i <= m; i++)
{
scanf("%d %d %d",&a, &b, &d);
p = new nod;
p -> x = a;
p -> d = d;
p -> a = v[b];
v[b] = p;
p = new nod;
p -> x = b;
p -> d = d;
p -> a = v[a];
v[a] = p;
}
}
void DF(int nod, int dist)
{
pNod p;
if (nod == y)
{
printf("%d\n",dist);
exit(0);
}
viz[nod] = 1;
for (p = v[nod]; p != NULL; p = p -> a)
{
if (!viz[p->x])
if (p -> x < nod)
{
dist -= p -> d;
DF(p -> x, dist);
dist += p -> d;
}
else
{
dist += p -> d;
DF(p -> x, dist);
dist -= p -> d;
}
}
}
int main()
{
citire();
if (x > y)
{
int aux = x;
x = y;
y = aux;
}
DF(x,0);
return 0;
}