arujbansal

CF 46 Q4

Sep 4th, 2021
1,344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void dbg_out() { cerr << endl; }
  6. template<typename Head, typename... Tail>
  7. void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
  8. #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
  9.  
  10. #define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
  11. #define rng_seed(x) mt19937 rng(x)
  12. #define all(x) (x).begin(), (x).end()
  13. #define sz(x) (int) (x).size()
  14. #define int long long
  15.  
  16. const int MXN = 1e5 + 5, INF = 1e18;
  17.  
  18. void solve() {
  19.     int M, N, S, H;
  20.     cin >> M >> N >> S >> H;
  21.     S--, H--;
  22.  
  23.     vector<pair<int, int>> g[MXN];
  24.  
  25.     for (int i = 0; i < N; i++) {
  26.         int u, v, w;
  27.         cin >> u >> v >> w;
  28.         u--, v--;
  29.  
  30.         g[u].emplace_back(v, w);
  31.         g[v].emplace_back(u, w);
  32.     }
  33.  
  34.     priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
  35.     vector<int> dist(M + 1, INF);
  36.  
  37.     dist[S] = 0;
  38.     pq.emplace(0, S);
  39.  
  40.     while (!pq.empty()) {
  41.         auto [cur_dist, u] = pq.top();
  42.         pq.pop();
  43.  
  44.         if (cur_dist != dist[u]) continue;
  45.  
  46.         for (const auto &[v, wt] : g[u]) {
  47.             int new_dist = cur_dist + wt;
  48.  
  49.             if (new_dist < dist[v]) {
  50.                 dist[v] = new_dist;
  51.                 pq.emplace(new_dist, v);
  52.             }
  53.         }
  54.     }
  55.    
  56.     cout << dist[H];
  57. }
  58.  
  59. signed main() {
  60.     ios_base::sync_with_stdio(false);
  61.     cin.tie(nullptr);
  62.  
  63.     int TC = 1;
  64.     // cin >> TC;
  65.     while (TC--) solve();
  66. }
Advertisement
Add Comment
Please, Sign In to add comment