Pagini recente » Cod sursa (job #1248041) | Cod sursa (job #1795046) | Rating Ioan Victor (Victor_Ioan) | Cod sursa (job #597656) | Cod sursa (job #350092)
Cod sursa(job #350092)
#include<stdio.h>
short int a[1<<10][1<<10];
int c[1<<10][1<<10];
int d[1<<10];
int n,m;
short int x,Y;
short int q[30002];
void read()
{
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
scanf("%d%d%hd%hd",&n,&m,&x,&Y);
int i,cost;
short int cx,cy;
for(i=1;i<=m;i++)
{
scanf("%hd%hd%d",&cx,&cy,&cost);
a[cx][++a[cx][0]]=cy;
a[cy][++a[cy][0]]=cx;
c[cx][++c[cx][0]]=cost;
c[cy][++c[cy][0]]=cost;
}
}
void rez()
{
int i,cost,p=1,u=0,lim;
short int y;
q[++u]=x;
while(d[Y]==0)
{
x=q[p++];
lim=a[x][0];
for(i=1;i<=lim;i++)
{
y=a[x][i];
if(d[y]==0)
{
cost=c[x][i];
if(x<y)
d[y]=d[x]+cost;
else
d[y]=d[x]-cost;
q[++u]=y;
}
}
}
printf("%d\n",d[Y]);
}
int main()
{
read();
rez();
return 0;
}