#include <bits/stdc++.h> using namespace std; using Z = pair<int, int64_t>; int k, m; vector<vector<Z>> z_arr; void Read() { int n; scanf("%d%d%d", &n, &k, &m); z_arr.resize(k); for (int i = 0; i < n; i++) { int ki, mi, ci; scanf("%d%d%d", &ki, &mi, &ci); z_arr[ki - 1].emplace_back(mi % m, ci); } } void MovesAddColor(vector<int64_t>& a, const vector<Z>& z) { vector<int64_t> res(m, -1); for (auto [w, c] : z) for (int r = 0; r < m; r++) if (a[r] >= 0) { int rr = (r + w) % m; int64_t cc = a[r] + c; if (res[rr] < 0 || cc < res[rr]) res[rr] = cc; } a = res; } vector<int64_t> Moves() { vector<int64_t> res(m, -1); res[0] = 0; for (const auto& v : z_arr) MovesAddColor(res, v); return res; } vector<int64_t> Solve() { vector<int64_t> moves = Moves(); vector<int64_t> res(m, -1); res[0] = 0; vector<int> avail_r(m); for (int i = 0; i < m; i++) avail_r[i] = i; for (;;) { int r_last = -1; { int best = -1; int i_best = -1; for (int i = 0; i < (int) avail_r.size(); i++) { int r = avail_r[i]; if (res[r] >= 0 && (best < 0 || res[r] < best)) { r_last = r; best = res[r]; i_best = i; } } if (i_best < 0) break; avail_r[i_best] = avail_r.back(); avail_r.pop_back(); } assert(r_last >= 0); for (int r = 0; r < m; r++) { if (moves[r] < 0) continue; int rr = (r_last + r) % m; int64_t cc = res[r_last] + moves[r]; if (res[rr] < 0 || cc < res[rr]) res[rr] = cc; } } return res; } int main() { Read(); for (int64_t c : Solve()) printf("%ld\n", c); 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 | #include <bits/stdc++.h> using namespace std; using Z = pair<int, int64_t>; int k, m; vector<vector<Z>> z_arr; void Read() { int n; scanf("%d%d%d", &n, &k, &m); z_arr.resize(k); for (int i = 0; i < n; i++) { int ki, mi, ci; scanf("%d%d%d", &ki, &mi, &ci); z_arr[ki - 1].emplace_back(mi % m, ci); } } void MovesAddColor(vector<int64_t>& a, const vector<Z>& z) { vector<int64_t> res(m, -1); for (auto [w, c] : z) for (int r = 0; r < m; r++) if (a[r] >= 0) { int rr = (r + w) % m; int64_t cc = a[r] + c; if (res[rr] < 0 || cc < res[rr]) res[rr] = cc; } a = res; } vector<int64_t> Moves() { vector<int64_t> res(m, -1); res[0] = 0; for (const auto& v : z_arr) MovesAddColor(res, v); return res; } vector<int64_t> Solve() { vector<int64_t> moves = Moves(); vector<int64_t> res(m, -1); res[0] = 0; vector<int> avail_r(m); for (int i = 0; i < m; i++) avail_r[i] = i; for (;;) { int r_last = -1; { int best = -1; int i_best = -1; for (int i = 0; i < (int) avail_r.size(); i++) { int r = avail_r[i]; if (res[r] >= 0 && (best < 0 || res[r] < best)) { r_last = r; best = res[r]; i_best = i; } } if (i_best < 0) break; avail_r[i_best] = avail_r.back(); avail_r.pop_back(); } assert(r_last >= 0); for (int r = 0; r < m; r++) { if (moves[r] < 0) continue; int rr = (r_last + r) % m; int64_t cc = res[r_last] + moves[r]; if (res[rr] < 0 || cc < res[rr]) res[rr] = cc; } } return res; } int main() { Read(); for (int64_t c : Solve()) printf("%ld\n", c); return 0; } |