Pagini recente » Cod sursa (job #2737724) | Cod sursa (job #1553604) | Cod sursa (job #1842902) | Cod sursa (job #867392) | Cod sursa (job #229821)
Cod sursa(job #229821)
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<algorithm>
using namespace std;
#define mp make_pair
#define fs first
#define sc second
struct graf
{
int n,d;
};
int n,m,x,y;
graf *a[30010];
int cat[30010];
void citire()
{
scanf("%d%d%d%d",&n,&m,&x,&y);
int n1,n2,d;
for(; m; m--)
{
scanf("%d%d%d",&n1,&n2,&d);
if((cat[n1]&7)==0)
a[n1]=(graf*)realloc(a[n1],(cat[n1]+10)*sizeof(graf));
if((cat[n2]&7)==0)
a[n2]=(graf*)realloc(a[n2],(cat[n2]+10)*sizeof(graf));
a[n1][cat[n1]].n=n2;
a[n1][cat[n1]++].d=d;
a[n2][cat[n2]].n=n1;
a[n2][cat[n2]++].d=d;
}
if(x>y)
swap(x,y);
}
void bfs()
{
queue < pair<int,int> > q;
q.push(mp(x,0));
bool incoada[30010]={0};
incoada[x]=true;
pair<int,int> now;
int i,next;
while(!q.empty())
{
now=q.front();
q.pop();
for(i=0; i<cat[now.fs]; i++)
{
next=a[now.fs][i].n;
if(next==y)
{
if(y>now.fs)
printf("%d\n",now.sc+a[now.fs][i].d);
else
printf("%d\n",now.sc-a[now.fs][i].d);
exit(0);
}
if(!incoada[next])
{
incoada[next]=true;
if(next>now.fs)
q.push(mp(next,now.sc+a[now.fs][i].d));
else
q.push(mp(next,now.sc-a[now.fs][i].d));
}
}
}
}
int main()
{
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
citire();
bfs();
return 0;
}