Pagini recente » Cod sursa (job #1253022) | Cod sursa (job #378544) | Cod sursa (job #1882449) | Cod sursa (job #2899776) | Cod sursa (job #2395569)
#include <bits/stdc++.h>
using namespace std;
const int NMAX=3*1e4+5;
int n,m,x,y,dist[NMAX];
vector< pair<int,int> > G[NMAX];
queue <int> q;
void citire()
{
scanf("%d%d%d%d",&n,&m,&x,&y);
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
G[a].push_back({b,c});
G[b].push_back({a,-c});
}
}
void bfs()
{
q.push(x);
dist[x]=1;
if(x==y) return ;
while(!q.empty())
{
int nods=q.front();
q.pop();
for(int i=0;i<G[nods].size();i++)
{
if(!dist[G[nods][i].first])
{
dist[G[nods][i].first]=dist[nods]+G[nods][i].second;
q.push(G[nods][i].first);
if(G[nods][i].first==y) break;
}
}
}
}
int main()
{
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
citire();
bfs();
printf("%d",dist[y]-1);
return 0;
}