Pagini recente » Cod sursa (job #2276740) | Cod sursa (job #2072253) | Cod sursa (job #2111057) | Cod sursa (job #2803480) | Cod sursa (job #2395579)
#include <bits/stdc++.h>
using namespace std;
const int NMAX=1<<15;
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);
int a,b,c;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
G[a].push_back({b,c});
G[b].push_back({a,-c});
}
}
void bfs()
{
int nods,nod,cost;
q.push(x);
dist[x]=1;
if(x==y) return ;
while(!q.empty())
{
nods=q.front();
for(vector<pair<int,int> >::iterator it=G[nods].begin();it!=G[nods].end();it++)
{
nod=it->first;
cost=it->second;
if(!dist[nod])
{
dist[nod]=dist[nods]+cost;
q.push(nod);
if(nod==y) return;
}
}
q.pop();
}
}
int main()
{
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
citire();
bfs();
printf("%d",dist[y]-1);
return 0;
}