Pagini recente » Cod sursa (job #644308) | Clasamentul arhivei de probleme | Cod sursa (job #901180) | Clasamentul arhivei educationale | Cod sursa (job #558763)
Cod sursa(job #558763)
#include <stdio.h>
#include <vector>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#include <queue>
#define maxn 30005
#define oo 200000000
using namespace std;
int i,N,M,X,Y;
vector< pair<int,int> > A[maxn];
vector< pair<int,int> > :: iterator it;
queue<int> Q;
int v[maxn],D[maxn];
void citire()
{
int x,y,z,aux;
scanf("%d %d %d %d",&N,&M,&X,&Y);
if(X>Y)
{
aux=X; X=Y; Y=aux;
}
for(i=1;i<=M;i++)
{
scanf("%d %d %d",&x,&y,&z);
A[x].pb(mp(y,z));
A[y].pb(mp(x,-z));
}
}
void BF()
{
int k;
for(i=1;i<=N;i++) D[i]=oo;
Q.push(X); v[X]=1; D[X]=0;
for(;!Q.empty();Q.pop())
{
k=Q.front();
for(it=A[k].begin();it!=A[k].end();it++)
if(D[k]+it->ss<D[it->ff])
{
D[it->ff]=D[k]+it->ss;
if(it->ff==Y) return;
if(!v[it->ff])
{
v[it->ff]=1;
Q.push(it->ff);
}
}
v[k]=0;
}
}
int main()
{
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
citire();
BF();
printf("%d",D[Y]);
}