#include <bits/stdc++.h> using namespace std; typedef long long LL; static vector<pair<int, LL>> Z[7009]; bool cmp_Z(vector<pair<int, LL>> &a, vector<pair<int, LL>> &b) { return a.size() < b.size(); } vector<pair<int, LL>> step; int n, k; static LL odl[7009]; static LL odl2[7009]; static int xxx[7009]; const LL INF = __LONG_LONG_MAX__ / 4; pair<LL, int> A1[5000009]; pair<LL, int> A2[5000009]; template <typename T> class my_queue { public: my_queue() { // this->A.resize(100009); this->size = 0; } bool empty() { if (size == 0) return true; return false; } T top(pair<LL, int> *A) { return A[0]; } void pop(pair<LL, int> *A) { this->size--; swap(A[0], A[size]); maxheapify(0, A); } void push(T val, pair<LL, int> *A) { A[size] = val; this->size++; add(size - 1, A); } my_queue &operator=(const my_queue &Q) { this->size = Q.size; // this->A = Q.A; return *this; } void clear() { this->size = 0; } int size2() { return this->size; } private: int size; // vector<T> A; static inline int left(int i) { return (i << 1) + 1; } static inline int right(int i) { return (i << 1) + 2; } void add(int i, pair<LL, int> *A) { if (i == 0) return; int parent = i >>= 1; if (A[parent] < A[i]) { swap(A[parent], A[i]); add(parent, A); } } void maxheapify(int i, pair<LL, int> *A) { int l = left(i); int r = right(i); int largest = i; if (l < size && A[l] > A[largest]) { largest = l; } if (r < size && A[r] > A[largest]) { largest = r; } if (largest != i) { swap(A[i], A[largest]); maxheapify(largest, A); } } }; bool dijkstra1(const int m) { for (int i = 1; i < m; i++) { odl[i] = INF; odl2[i] = INF; } odl2[0] = INF; /*priority_queue<pair<LL, int>> q; priority_queue<pair<LL, int>> qq; priority_queue<pair<LL, int>> qempty; */ my_queue<pair<LL, int>> q; my_queue<pair<LL, int>> qq; q.push({0, 0}, A1); for (int i = 0; i < k; ++i) { while (!q.empty()) { const auto [tmp, x] = q.top(A1); q.pop(A1); if (odl[x] < -tmp) { continue; } for (const auto &[M, C] : Z[i]) { const int Y = (x + M) % m; if (odl[x] + C < odl2[Y]) { odl2[Y] = odl[x] + C; qq.push({-odl2[Y], Y}, A2); xxx[Y] = 1; } } } if (qq.empty()) { return false; } q = qq; for (int i = 0; i < q.size2(); ++i) { A1[i] = A2[i]; } qq.clear(); // qq = qempty; for (int i = 0; i < m; ++i) { if (xxx[i]) { odl[i] = odl2[i]; } else { odl[i] = INF; } odl2[i] = INF; xxx[i] = 0; } } while (!q.empty()) { auto [tmp, x] = q.top(A1); q.pop(A1); step.emplace_back(x, odl[x]); } return true; } void dijkstra2(const int m) { odl[0] = 0; for (int i = 1; i < m; ++i) { odl[i] = INF; } priority_queue<pair<LL, int>> q; q.push({0, 0}); while (!q.empty()) { const auto [tmp, x] = q.top(); q.pop(); if (odl[x] < -tmp) { continue; } for (const auto &[st, nd] : step) { const int Y = (x + st) % m; if (odl[x] + nd < odl[Y]) { odl[Y] = odl[x] + nd; q.push({-odl[Y], Y}); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int m; cin >> n >> k >> m; for (int i = 0; i < n; i++) { int a, b; LL c; cin >> a >> b >> c; Z[a - 1].emplace_back(b % m, c); } sort(Z, Z + k, cmp_Z); reverse(Z, Z + k); if (dijkstra1(m) == false) { cout << "0\n"; for (int i = 1; i < m; i++) { cout << "-1\n"; } return 0; } dijkstra2(m); for (int i = 0; i < m; i++) { if (odl[i] == INF) { cout << "-1\n"; } else { cout << odl[i] << "\n"; } } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | #include <bits/stdc++.h> using namespace std; typedef long long LL; static vector<pair<int, LL>> Z[7009]; bool cmp_Z(vector<pair<int, LL>> &a, vector<pair<int, LL>> &b) { return a.size() < b.size(); } vector<pair<int, LL>> step; int n, k; static LL odl[7009]; static LL odl2[7009]; static int xxx[7009]; const LL INF = __LONG_LONG_MAX__ / 4; pair<LL, int> A1[5000009]; pair<LL, int> A2[5000009]; template <typename T> class my_queue { public: my_queue() { // this->A.resize(100009); this->size = 0; } bool empty() { if (size == 0) return true; return false; } T top(pair<LL, int> *A) { return A[0]; } void pop(pair<LL, int> *A) { this->size--; swap(A[0], A[size]); maxheapify(0, A); } void push(T val, pair<LL, int> *A) { A[size] = val; this->size++; add(size - 1, A); } my_queue &operator=(const my_queue &Q) { this->size = Q.size; // this->A = Q.A; return *this; } void clear() { this->size = 0; } int size2() { return this->size; } private: int size; // vector<T> A; static inline int left(int i) { return (i << 1) + 1; } static inline int right(int i) { return (i << 1) + 2; } void add(int i, pair<LL, int> *A) { if (i == 0) return; int parent = i >>= 1; if (A[parent] < A[i]) { swap(A[parent], A[i]); add(parent, A); } } void maxheapify(int i, pair<LL, int> *A) { int l = left(i); int r = right(i); int largest = i; if (l < size && A[l] > A[largest]) { largest = l; } if (r < size && A[r] > A[largest]) { largest = r; } if (largest != i) { swap(A[i], A[largest]); maxheapify(largest, A); } } }; bool dijkstra1(const int m) { for (int i = 1; i < m; i++) { odl[i] = INF; odl2[i] = INF; } odl2[0] = INF; /*priority_queue<pair<LL, int>> q; priority_queue<pair<LL, int>> qq; priority_queue<pair<LL, int>> qempty; */ my_queue<pair<LL, int>> q; my_queue<pair<LL, int>> qq; q.push({0, 0}, A1); for (int i = 0; i < k; ++i) { while (!q.empty()) { const auto [tmp, x] = q.top(A1); q.pop(A1); if (odl[x] < -tmp) { continue; } for (const auto &[M, C] : Z[i]) { const int Y = (x + M) % m; if (odl[x] + C < odl2[Y]) { odl2[Y] = odl[x] + C; qq.push({-odl2[Y], Y}, A2); xxx[Y] = 1; } } } if (qq.empty()) { return false; } q = qq; for (int i = 0; i < q.size2(); ++i) { A1[i] = A2[i]; } qq.clear(); // qq = qempty; for (int i = 0; i < m; ++i) { if (xxx[i]) { odl[i] = odl2[i]; } else { odl[i] = INF; } odl2[i] = INF; xxx[i] = 0; } } while (!q.empty()) { auto [tmp, x] = q.top(A1); q.pop(A1); step.emplace_back(x, odl[x]); } return true; } void dijkstra2(const int m) { odl[0] = 0; for (int i = 1; i < m; ++i) { odl[i] = INF; } priority_queue<pair<LL, int>> q; q.push({0, 0}); while (!q.empty()) { const auto [tmp, x] = q.top(); q.pop(); if (odl[x] < -tmp) { continue; } for (const auto &[st, nd] : step) { const int Y = (x + st) % m; if (odl[x] + nd < odl[Y]) { odl[Y] = odl[x] + nd; q.push({-odl[Y], Y}); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int m; cin >> n >> k >> m; for (int i = 0; i < n; i++) { int a, b; LL c; cin >> a >> b >> c; Z[a - 1].emplace_back(b % m, c); } sort(Z, Z + k, cmp_Z); reverse(Z, Z + k); if (dijkstra1(m) == false) { cout << "0\n"; for (int i = 1; i < m; i++) { cout << "-1\n"; } return 0; } dijkstra2(m); for (int i = 0; i < m; i++) { if (odl[i] == INF) { cout << "-1\n"; } else { cout << odl[i] << "\n"; } } return 0; } |