Pagini recente » Cod sursa (job #3346215) | Cod sursa (job #3340886) | Cod sursa (job #2190696) | Cod sursa (job #2191099) | Cod sursa (job #3340788)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int n,m,x,y;
bool gy;
vector<vector<pair<int,int>>>a;
vector<bool> viz;
void bfs(int st) {
queue<pair<int,int>>q;
q.push({st,0});
while (!q.empty()) {
int nod=q.front().first,dist=q.front().second;
q.pop();
if (nod==y) {
gy=true;
fout<<dist;
}
for (auto f:a[nod]) {
if (!viz[f.first]) {
if (f.first>nod) q.push({f.first,dist+f.second});
else q.push({f.first,dist-f.second});
viz[f.first]=true;
}
}
}
}
int main() {
fin>>n>>m>>x>>y;
a.resize(n+1);
viz.resize(n+1);
int na,nb,nc;
for (int i=1;i<=n;i++) {
fin>>na>>nb>>nc;
a[na].push_back({nb,nc});
a[nb].push_back({na,nc});
}
bfs(x);
if (!gy) fout<<"-1";
return 0;
}