Pagini recente » Cod sursa (job #3273464) | Cod sursa (job #1185058) | Cod sursa (job #126688) | Cod sursa (job #405724) | Cod sursa (job #3221668)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
// ifstream cin("input.txt"); ofstream cout("output.txt");
ifstream cin("sate.in"); ofstream cout("sate.out");
int main() {
int n, m, x, y;
cin>>n>>m>>x>>y;
vector<vector<pair<int,int>>> gr(n+1);
int a, b, d;
while (m--){
cin>>a>>b>>d;
gr[a].push_back({b, d});
gr[b].push_back({a, -d});
}
vector<bool> used(n+1, false);
queue<pair<int,int>> q;
q.push({x, 0});
used[x] = true;
while (!q.empty()){
pair<int,int> now = q.front();
q.pop();
if (now.first == y){
cout<<now.second<<'\n';
break;
}
for (auto &x : gr[now.first]){
if (!used[x.first]){
used[x.first] = true;
q.push({x.first, now.second + x.second});
}
}
}
return 0;
}