Pagini recente » Cod sursa (job #1813776) | Cod sursa (job #781993) | Cod sursa (job #259687) | Cod sursa (job #759647) | Cod sursa (job #183382)
Cod sursa(job #183382)
#include <stdio.h>
using namespace std;
#define dim 30010
struct nod
{
int inf;
long cost;
nod *next;
};
nod *l[dim];
int s[dim], sir[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 bf(int );
int main()
{
freopen("file.in", "r", stdin);
freopen("file.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);
}
bf(X);
return 0;
}
void bf(int x)
{
long in, sf, ok=0, cost[dim]={0};
in=sf=1;
sir[in]=x;
s[x]=1;
while(!ok)
{
nod *p=l[sir[in]];
int ind;
while(p)
{
ind=p->inf;
if(!s[ind])
{
sir[++sf]=ind;
s[ind]=1;
cost[sf]=cost[in]+p->cost;
if(ind==Y)
{
printf("%ld\n", cost[sf]);
ok=1;
break;
}
}
p=p->next;
}
in++;
}
}
/*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;
}
}
}
} */