#include <bits/stdc++.h> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const ll INF = static_cast<ll>(1e18) + 9LL; struct Gum { int weight; int cost; }; void inline one() { int n, max_color, max_weight; cin >> n >> max_color >> max_weight; vector<vector<Gum>> by_color(max_color + 1); REP(i, n) { int color, weight, cost; cin >> color >> weight >> cost; by_color[color].emplace_back(weight, cost); } vector<ll> dp(max_weight, INF); dp[0] = 0; FOR(color, 1, max_color) { // vector<ll> prev(dp.begin(), dp.end()); vector<ll> best(max_weight, INF); // fill(dp.begin(), dp.end(), INF); for (const auto &gum : by_color[color]) { // vector<ll> cand(max_weight, INF); REP(i, max_weight) { int j = (i + gum.weight) % max_weight; best[j] = min(best[j], dp[i] + gum.cost); } } dp = best; } dp[0] = 0; // REP(i, max_weight) { DEB("weight = " << i << ": dp=" << dp[i] << "\n"); } for (;;) { bool any = false; FOR(weight, 1, max_weight - 1) { REP(i, max_weight) { int j = (i + weight) % max_weight; ll value = dp[i] + dp[weight]; if (value < dp[j]) { dp[j] = value; any = true; } } } if (not any) { break; } } REP(i, max_weight) { ll result = dp[i]; if (result >= INF) { result = -1; } cout << result << "\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); }
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 | #include <bits/stdc++.h> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const ll INF = static_cast<ll>(1e18) + 9LL; struct Gum { int weight; int cost; }; void inline one() { int n, max_color, max_weight; cin >> n >> max_color >> max_weight; vector<vector<Gum>> by_color(max_color + 1); REP(i, n) { int color, weight, cost; cin >> color >> weight >> cost; by_color[color].emplace_back(weight, cost); } vector<ll> dp(max_weight, INF); dp[0] = 0; FOR(color, 1, max_color) { // vector<ll> prev(dp.begin(), dp.end()); vector<ll> best(max_weight, INF); // fill(dp.begin(), dp.end(), INF); for (const auto &gum : by_color[color]) { // vector<ll> cand(max_weight, INF); REP(i, max_weight) { int j = (i + gum.weight) % max_weight; best[j] = min(best[j], dp[i] + gum.cost); } } dp = best; } dp[0] = 0; // REP(i, max_weight) { DEB("weight = " << i << ": dp=" << dp[i] << "\n"); } for (;;) { bool any = false; FOR(weight, 1, max_weight - 1) { REP(i, max_weight) { int j = (i + weight) % max_weight; ll value = dp[i] + dp[weight]; if (value < dp[j]) { dp[j] = value; any = true; } } } if (not any) { break; } } REP(i, max_weight) { ll result = dp[i]; if (result >= INF) { result = -1; } cout << result << "\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); } |