Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void dbg_out() { cerr << endl; }
- template<typename Head, typename... Tail>
- void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
- #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
- #define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
- #define rng_seed(x) mt19937 rng(x)
- #define all(x) (x).begin(), (x).end()
- #define sz(x) (int) (x).size()
- #define int long long
- const int MXN = 1e5 + 5, INF = 1e18;
- void solve() {
- int M, N, S, H;
- cin >> M >> N >> S >> H;
- S--, H--;
- vector<pair<int, int>> g[MXN];
- for (int i = 0; i < N; i++) {
- int u, v, w;
- cin >> u >> v >> w;
- u--, v--;
- g[u].emplace_back(v, w);
- g[v].emplace_back(u, w);
- }
- priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
- vector<int> dist(M + 1, INF);
- dist[S] = 0;
- pq.emplace(0, S);
- while (!pq.empty()) {
- auto [cur_dist, u] = pq.top();
- pq.pop();
- if (cur_dist != dist[u]) continue;
- for (const auto &[v, wt] : g[u]) {
- int new_dist = cur_dist + wt;
- if (new_dist < dist[v]) {
- dist[v] = new_dist;
- pq.emplace(new_dist, v);
- }
- }
- }
- cout << dist[H];
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- int TC = 1;
- // cin >> TC;
- while (TC--) solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment