Pagini recente » Cod sursa (job #2242255) | Cod sursa (job #3161520) | Cod sursa (job #3200460) | Cod sursa (job #176271) | Cod sursa (job #801219)
Cod sursa(job #801219)
#include <stdio.h>
#include <vector>
#include <queue>
#define pb push_back
#define mp make_pair
using namespace std;
vector < pair <int, int> > g[30001];
queue <int> q;
int n,m,a,b,x,y,d,i,s,way[30001];
bool sel[30001];
void bf (int x)
{
int nod,i,nurm,c;
q.push(x); sel[x]=true;way[x]=0;
while (!q.empty())
{
nod=q.front();
for (i=0;i<g[nod].size();i++)
{
nurm=g[nod][i].first;
c=g[nod][i].second;
if ( !sel[nurm])
{
q.push(nurm);
sel[nurm]=true;
way[nurm]=way[nod]+c;
if (nurm==b) return;
}
}
q.pop();
}
}
int main()
{
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&a,&b);
for (i=1;i<=m;i++)
{
scanf("%d%d%d\n",&x,&y,&d);
g[x].pb(mp(y,d));
g[y].pb(mp(x,-d));
}
bf(a);
if (way[b]!=0) printf("%d\n",way[b]); else printf("-1\n");
return 0;
}