#include <bits/stdc++.h> using namespace std; const long long INF = 1LL << 60; void propagate(int m, vector <long long> &dp) { for (int r = 0; r < m; r++) if (dp[r] < INF) { long long dpTotal = dp[r], rTotal = r; for (int mult = 2; mult < m; mult++) { dpTotal += dp[r]; if (dpTotal > INF) { break; } rTotal += r; if (rTotal >= m) { rTotal -= m; } dp[rTotal] = min(dp[rTotal], dpTotal); } } vector <int> rems(m); iota(rems.begin(), rems.end(), 0); sort(rems.begin(), rems.end(), [&](int r1, int r2) { return dp[r1] < dp[r2]; }); for (int i1 = 0; i1 < m; i1++) for (int i2 = i1 + 1; i2 < m; i2++) { int r1 = rems[i1], r2 = rems[i2]; int rTotal = r1 + r2; if (rTotal >= m) { rTotal -= m; } long long dpTotal = dp[r1] + dp[r2]; dp[rTotal] = min(dp[rTotal], dpTotal); } } vector <long long> solve(int n, int k, int m, vector <int> &color, vector <int> &mass, vector <int> &cost) { vector <vector<int>> withColor(k); for (int i = 0; i < n; i++) { withColor[color[i]].push_back(i); } vector <long long> dp(m, INF); dp[0] = 0; for (int c = 0; c < k; c++) { vector <long long> dpNew(m, INF); for (int i : withColor[c]) { for (int r = 0; r < m; r++) { int rNew = r + mass[i]; if (rNew >= m) { rNew -= m; } long long costHere = dp[r] + cost[i]; dpNew[rNew] = min(dpNew[rNew], dp[r] + cost[i]); } } dp = dpNew; } dp[0] = 0; propagate(m, dp); return dp; } int main() { ios_base::sync_with_stdio(false); int n, k, m; cin >> n >> k >> m; vector <int> color(n), mass(n), cost(n); for (int i = 0; i < n; i++) { cin >> color[i] >> mass[i] >> cost[i]; color[i]--; mass[i] %= m; } auto ans = solve(n, k, m, color, mass, cost); for (auto x : ans) { if (x == INF) { cout << -1; } else { cout << x; } cout << '\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 | #include <bits/stdc++.h> using namespace std; const long long INF = 1LL << 60; void propagate(int m, vector <long long> &dp) { for (int r = 0; r < m; r++) if (dp[r] < INF) { long long dpTotal = dp[r], rTotal = r; for (int mult = 2; mult < m; mult++) { dpTotal += dp[r]; if (dpTotal > INF) { break; } rTotal += r; if (rTotal >= m) { rTotal -= m; } dp[rTotal] = min(dp[rTotal], dpTotal); } } vector <int> rems(m); iota(rems.begin(), rems.end(), 0); sort(rems.begin(), rems.end(), [&](int r1, int r2) { return dp[r1] < dp[r2]; }); for (int i1 = 0; i1 < m; i1++) for (int i2 = i1 + 1; i2 < m; i2++) { int r1 = rems[i1], r2 = rems[i2]; int rTotal = r1 + r2; if (rTotal >= m) { rTotal -= m; } long long dpTotal = dp[r1] + dp[r2]; dp[rTotal] = min(dp[rTotal], dpTotal); } } vector <long long> solve(int n, int k, int m, vector <int> &color, vector <int> &mass, vector <int> &cost) { vector <vector<int>> withColor(k); for (int i = 0; i < n; i++) { withColor[color[i]].push_back(i); } vector <long long> dp(m, INF); dp[0] = 0; for (int c = 0; c < k; c++) { vector <long long> dpNew(m, INF); for (int i : withColor[c]) { for (int r = 0; r < m; r++) { int rNew = r + mass[i]; if (rNew >= m) { rNew -= m; } long long costHere = dp[r] + cost[i]; dpNew[rNew] = min(dpNew[rNew], dp[r] + cost[i]); } } dp = dpNew; } dp[0] = 0; propagate(m, dp); return dp; } int main() { ios_base::sync_with_stdio(false); int n, k, m; cin >> n >> k >> m; vector <int> color(n), mass(n), cost(n); for (int i = 0; i < n; i++) { cin >> color[i] >> mass[i] >> cost[i]; color[i]--; mass[i] %= m; } auto ans = solve(n, k, m, color, mass, cost); for (auto x : ans) { if (x == INF) { cout << -1; } else { cout << x; } cout << '\n'; } return 0; } |