Pagini recente » Cod sursa (job #2689537) | Cod sursa (job #1003570) | Cod sursa (job #2112060) | Cod sursa (job #1131336) | Cod sursa (job #553885)
Cod sursa(job #553885)
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<fstream>
#include<stdlib.h>
using namespace std;
const int LGMAX = int(3e4)+5;
int viz[LGMAX], Q[LGMAX*2],
n,m,X,Y;
typedef struct nod
{
int inf,d;
nod *urm;
} TNOD;
TNOD *v[LGMAX],*p;
void add( int a, int b, int ds )
{
p = new TNOD;
p->inf = a;
p->d = ds;
p->urm = v[b];
v[b] = p;
}
void citire()
{
int i,x,y,ds;
ifstream in("sate.in");
in>>n>>m>>X>>Y;
for( i=1; i<=m; i++ )
{
in>>x>>y>>ds;
add( x, y, -ds );
add( y, x, ds );
}
if(X>Y)
{
int q = Y;
Y = X;
X = q;
}
}
int dst[LGMAX];
int bfs()
{
int fi=0,ls=0;
Q[ls++] = X;
memset(dst,-1,sizeof(dst));
dst[X] = 0;
while( fi<=ls )
{
for( p=v[Q[fi]]; p; p=p->urm )
if( dst[p->inf]==-1 )
dst[p->inf] = dst[Q[fi]] + p->d,
Q[ls++] = p->inf;
if(dst[Y]!=-1) return dst[Y];
fi++;
}
}
int main()
{
freopen("sate.out","w",stdout);
citire();
cout<<bfs()<<endl;
return 0;
}