|
| 1 | +#include <cstdio> |
| 2 | +#include <cstdlib> |
| 3 | +#include <cstring> |
| 4 | +#include <iostream> |
| 5 | +#include <algorithm> |
| 6 | +#include <climits> |
| 7 | +#include <vector> |
| 8 | +#include <queue> |
| 9 | +using namespace std; |
| 10 | +template<typename T, int maxsize> struct ModifiablePriorityQueue { // 大根堆 |
| 11 | + int data[maxsize * 2]; |
| 12 | + int pos[maxsize * 2]; |
| 13 | + T value[maxsize * 2]; |
| 14 | + int sz; |
| 15 | + ModifiablePriorityQueue() : pos(), sz(0) {} |
| 16 | + void up(int i) { // 值变大 |
| 17 | + auto index = data[i]; |
| 18 | + auto val = value[index]; |
| 19 | + while (i > 1) { |
| 20 | + int fa = i / 2; |
| 21 | + if (val > value[data[fa]]) { |
| 22 | + data[i] = data[fa]; |
| 23 | + pos[data[i]] = i; |
| 24 | + i = fa; |
| 25 | + } |
| 26 | + else { |
| 27 | + break; |
| 28 | + } |
| 29 | + } |
| 30 | + data[i] = index; |
| 31 | + pos[index] = i; |
| 32 | + } |
| 33 | + void down(int i) { // 值变小 |
| 34 | + auto index = data[i]; |
| 35 | + auto val = value[index]; |
| 36 | + while (i * 2 <= sz) { |
| 37 | + int child = i * 2; |
| 38 | + if (i * 2 + 1 <= sz && value[data[i * 2 + 1]] > value[data[child]]) { |
| 39 | + child = i * 2 + 1; |
| 40 | + } |
| 41 | + if (val < value[data[child]]) { |
| 42 | + data[i] = data[child]; |
| 43 | + pos[data[i]] = i; |
| 44 | + i = child; |
| 45 | + } |
| 46 | + else { |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + data[i] = index; |
| 51 | + pos[index] = i; |
| 52 | + } |
| 53 | + void push(int index, const T& val) { |
| 54 | + sz += 1; |
| 55 | + data[sz] = index; |
| 56 | + value[index] = val; |
| 57 | + up(sz); |
| 58 | + } |
| 59 | + void pop() { |
| 60 | + pos[data[1]] = 0; |
| 61 | + data[1] = data[sz]; |
| 62 | + sz -= 1; |
| 63 | + if (sz > 0) { |
| 64 | + down(1); |
| 65 | + } |
| 66 | + } |
| 67 | + void erase(int index) { |
| 68 | + index = pos[index]; |
| 69 | + pos[data[index]] = 0; |
| 70 | + data[index] = data[sz]; |
| 71 | + sz -= 1; |
| 72 | + if (index <= sz) { |
| 73 | + down(index); |
| 74 | + } |
| 75 | + } |
| 76 | + void modify(int index, const T& val) { |
| 77 | + if (pos[index] == 0) { |
| 78 | + push(index, val); |
| 79 | + } |
| 80 | + else if (val > value[index]) { |
| 81 | + value[index] = val; |
| 82 | + up(pos[index]); |
| 83 | + } |
| 84 | + else { |
| 85 | + value[index] = val; |
| 86 | + down(pos[index]); |
| 87 | + } |
| 88 | + } |
| 89 | + int top() const { |
| 90 | + return data[1]; |
| 91 | + } |
| 92 | + T get(int index) const { |
| 93 | + return value[index]; |
| 94 | + } |
| 95 | + T maximum() const { |
| 96 | + return value[data[1]]; |
| 97 | + } |
| 98 | + bool contains(int index) const { |
| 99 | + return pos[index] != 0; |
| 100 | + } |
| 101 | + int size() const { |
| 102 | + return sz; |
| 103 | + } |
| 104 | + bool empty() const { |
| 105 | + return sz == 0; |
| 106 | + } |
| 107 | +}; |
| 108 | +int main() { // 洛谷 P4779 |
| 109 | + static int d[210000]; |
| 110 | + static vector<pair<int, int>> G[210000]; |
| 111 | + int n, m, s; |
| 112 | + scanf("%d %d %d", &n, &m, &s); |
| 113 | + for (int i = 0; i < m; ++i) { |
| 114 | + int x, y, z; |
| 115 | + scanf("%d %d %d", &x, &y, &z); |
| 116 | + G[x].emplace_back(y, z); |
| 117 | + } |
| 118 | + static ModifiablePriorityQueue<int, 210000> Q; |
| 119 | + memset(d, 0x3f, sizeof(d)); |
| 120 | + d[s] = 0; |
| 121 | + Q.push(s, 0); |
| 122 | + while (!Q.empty()) { |
| 123 | + auto x = Q.top(); Q.pop(); |
| 124 | + for (auto [y, w] : G[x]) { |
| 125 | + if (d[y] > d[x] + w) { |
| 126 | + d[y] = d[x] + w; |
| 127 | + Q.modify(y, -d[y]); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + for (int i = 1; i <= n; ++i) { |
| 132 | + printf("%d ", d[i] == 0x3f3f3f3f ? INT_MAX : d[i]); |
| 133 | + } |
| 134 | + printf("\n"); |
| 135 | + return 0; |
| 136 | +} |
0 commit comments