Pagini recente » Cod sursa (job #3349530) | Cod sursa (job #3320119) | Cod sursa (job #1835142) | Cod sursa (job #3300867) | Cod sursa (job #3330581)
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
int N,M,X,Y;cin>>N>>M>>X>>Y; N++;
vector<pair<int,int>> adj[N];
bool vis[N];
for(int i=0;i<N;i++){
vis[i]=false;
}
int n,a,b,d;
for(int i=0;i<M;i++){
cin>>a>>b>>d;
adj[a].push_back({b,d});
adj[b].push_back({a,-d});
}
queue<pair<int,int>> q;
q.push({X,0});
while(!q.empty()){
n=q.front().first;d=q.front().second;
q.pop();
if(vis[d])continue;
if(n==Y){
cout<<d;
return 0;
}
vis[d]=true;
for(auto x: adj[n]){
q.push({x.first,d+x.second});
}
}
}