Pagini recente » Cod sursa (job #135055) | Cod sursa (job #1431189) | Istoria paginii utilizator/pedape6453 | Cod sursa (job #385938) | Cod sursa (job #183085)
Cod sursa(job #183085)
#include <stdio.h>
using namespace std;
#define dim 30010
struct nod
{
int inf;
long cost;
nod *next;
};
nod *l[dim];
int s[dim];
long gasit, n, m, sum, i, X, Y;
void add(int x, int y, long c)
{
if(x>y)
c*=-1;
nod *p=new nod;
p->inf=y;
p->cost=c;
p->next=l[x];
l[x]=p;
}
void df(int );
int main()
{
freopen("sate.in", "r", stdin);
freopen("sate.out", "w", stdout);
int x, y; long c;
scanf("%ld %ld %ld %ld", &n, &m, &X, &Y);
for(i=1; i<=m; i++)
{
scanf("%d %d %ld", &x, &y, &c);
add(x, y, c);
add(y, x, c);
}
df(X);
return 0;
}
void df(int x)
{
if(!gasit)
{
if(x==Y)
{
gasit=1;
if(sum<0)
sum*=-1;
printf("%ld\n", sum);
}
else
{
int ind;
nod *p=l[x];
while(p)
{
ind=p->inf;
if(!s[ind])
{
s[ind]=1;
sum+=p->cost;
df(ind);
s[ind]=0;
sum-=p->cost;
}
p=p->next;
}
}
}
}