fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. static inline ll readLL() {
  6. ll x = 0; int c = getchar(); bool neg = false;
  7. while (c != '-' && (c < '0' || c > '9')) c = getchar();
  8. if (c == '-') { neg = true; c = getchar(); }
  9. while (c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = getchar(); }
  10. return neg ? -x : x;
  11. }
  12.  
  13. const int N = 2e5 + 5;
  14. const ll oo = (ll)1e18;
  15.  
  16. int n, m, a, b;
  17. int head_[N], nxt_[2 * N], to_[2 * N];
  18. ll w_[2 * N];
  19. int ecnt = 0;
  20. inline void addEdge(int u, int v, ll w) {
  21. to_[ecnt] = v; w_[ecnt] = w; nxt_[ecnt] = head_[u]; head_[u] = ecnt++;
  22. }
  23.  
  24. struct FibHeapArr {
  25. ll key[N];
  26. int deg[N], par[N], child[N], left[N], right[N];
  27. bool mark[N], inHeap[N];
  28. int mn = 0;
  29.  
  30. void init(int sz) {
  31. mn = 0;
  32. for (int i = 0; i <= sz; i++) {
  33. par[i] = child[i] = 0;
  34. left[i] = right[i] = i;
  35. deg[i] = 0; mark[i] = false; inHeap[i] = false;
  36. key[i] = oo;
  37. }
  38. }
  39.  
  40. inline void listRemove(int x) {
  41. left[right[x]] = left[x];
  42. right[left[x]] = right[x];
  43. left[x] = right[x] = x;
  44. }
  45.  
  46. inline void addRoot(int x) {
  47. par[x] = 0; mark[x] = false;
  48. if (!mn) { mn = x; left[x] = right[x] = x; return; }
  49. left[x] = mn; right[x] = right[mn];
  50. left[right[mn]] = x; right[mn] = x;
  51. if (key[x] < key[mn]) mn = x;
  52. }
  53.  
  54. inline bool empty() const { return mn == 0; }
  55.  
  56. void insert(int x, ll k) {
  57. key[x] = k; deg[x] = 0; mark[x] = false;
  58. par[x] = child[x] = 0; left[x] = right[x] = x;
  59. inHeap[x] = true;
  60. addRoot(x);
  61. }
  62.  
  63. inline void link(int x, int y) {
  64. listRemove(x);
  65. par[x] = y; mark[x] = false;
  66. if (!child[y]) { child[y] = x; left[x] = right[x] = x; }
  67. else {
  68. int c = child[y];
  69. left[x] = c; right[x] = right[c];
  70. left[right[c]] = x; right[c] = x;
  71. }
  72. deg[y]++;
  73. }
  74.  
  75. int degTable[40];
  76. void consolidate() {
  77. int cnt = 0;
  78. int p = mn;
  79. static int roots[N];
  80. do { roots[cnt++] = p; p = right[p]; } while (p != mn);
  81.  
  82. memset(degTable, 0, sizeof(degTable));
  83. for (int i = 0; i < cnt; i++) {
  84. int x = roots[i];
  85. int d = deg[x];
  86. while (degTable[d]) {
  87. int y = degTable[d];
  88. if (key[x] > key[y]) swap(x, y);
  89. link(y, x);
  90. degTable[d] = 0;
  91. d = deg[x];
  92. }
  93. degTable[d] = x;
  94. }
  95.  
  96. mn = 0;
  97. for (int i = 0; i < 40; i++) if (degTable[i]) {
  98. int x = degTable[i];
  99. left[x] = right[x] = x;
  100. addRoot(x);
  101. }
  102. }
  103.  
  104. pair<int, ll> popMin() {
  105. int z = mn;
  106. pair<int, ll> res = {z, key[z]};
  107.  
  108. if (child[z]) {
  109. int c = child[z];
  110. vector<int> kids;
  111. int p = c;
  112. do { kids.push_back(p); p = right[p]; } while (p != c);
  113. for (int x : kids) { listRemove(x); addRoot(x); }
  114. }
  115. child[z] = 0; deg[z] = 0;
  116.  
  117. if (right[z] == z) mn = 0;
  118. else { int p = right[z]; listRemove(z); mn = p; }
  119.  
  120. inHeap[z] = false;
  121. if (mn) consolidate();
  122. return res;
  123. }
  124.  
  125. void cut(int x, int y) {
  126. if (right[x] == x) child[y] = 0;
  127. else { if (child[y] == x) child[y] = right[x]; listRemove(x); }
  128. deg[y]--;
  129. addRoot(x);
  130. }
  131.  
  132. void cascadingCut(int x) {
  133. int y = par[x];
  134. if (!y) return;
  135. if (!mark[x]) mark[x] = true;
  136. else { cut(x, y); cascadingCut(y); }
  137. }
  138.  
  139. void decreaseKey(int x, ll k) {
  140. key[x] = k;
  141. int y = par[x];
  142. if (y && key[x] < key[y]) { cut(x, y); cascadingCut(y); }
  143. if (key[x] < key[mn]) mn = x;
  144. }
  145. };
  146.  
  147. FibHeapArr pq;
  148.  
  149. ll dista[N];
  150. int trace_[N];
  151. vector<int> path;
  152.  
  153. void sol(){
  154. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  155. cin >> n >> m >> a >> b;
  156. memset(head_, -1, sizeof(int) * (n + 1));
  157.  
  158. for (int i = 0; i < m; i++) {
  159. int u, v; ll w; cin >> u >> v >> w;
  160. addEdge(u, v, w);
  161. addEdge(v, u, w);
  162. }
  163.  
  164. fill(dista, dista + n + 1, oo);
  165. fill(trace_, trace_ + n + 1, -1);
  166. pq.init(n);
  167.  
  168. dista[a] = 0;
  169. pq.insert(a, 0);
  170.  
  171. while (!pq.empty()) {
  172. auto [u, du] = pq.popMin();
  173. if (u == b) break;
  174.  
  175. for (int e = head_[u]; e != -1; e = nxt_[e]) {
  176. int v = to_[e];
  177. ll nd = du + w_[e];
  178. if (nd < dista[v]) {
  179. dista[v] = nd;
  180. trace_[v] = u;
  181. if (!pq.inHeap[v]) pq.insert(v, nd);
  182. else pq.decreaseKey(v, nd);
  183. }
  184. }
  185. }
  186.  
  187. cout << (dista[b] == oo ? 0 : dista[b]);
  188. }
  189.  
  190. int main() {
  191. sol();
  192. return 0;
  193. }
  194.  
Success #stdin #stdout 0s 10692KB
stdin
Standard input is empty
stdout
Standard output is empty