Pagini recente » Cod sursa (job #2333851) | Cod sursa (job #2468947) | Cod sursa (job #1050699) | Cod sursa (job #2588631) | Cod sursa (job #2216103)
#include <bits/stdc++.h>
using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");
const int NMAX = 30005;
vector<pair<int, int> > g[NMAX];
queue<pair<int, int> > q;
bool visited[NMAX];
int main() {
ios::sync_with_stdio(false);
int n, m, x, y;
in >> n >> m >> x >> y;
for (int i = 1; i <= m; i ++) {
int a, b, c;
in >> a >> b >> c;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
q.push({x, 0});
visited[x] = 1;
while (q.size()) {
auto s = q.front();
q.pop();
if(s.first == y) {
out << s.second;
return 0;
}
for(auto i : g[s.first]) {
if(!visited[i.first]) {
visited[i.first] = 1;
int aux = s.second;
if(i.first < s.first)
aux -= i.second;
else
aux += i.second;
q.push({i.first, aux});
}
}
}
return 0;
}