#include <bits/stdc++.h> using namespace std; #define endl '\n' #define L long long #define MP make_pair #define REP(i, n) for(int i = 0; i < n; ++i) #define REPR(i, n) for(int i = n - 1; i >= 0; --i) #define FOR(i, a, b) for(int i = a; i < b; ++i) #define FORR(i, a, b) for(int i = b - 1; i >= a; --i) #define EB emplace_back #define ST first #define ND second #define S size #define RS resize template<class T> using P = pair<T, T>; template<class T> using V = vector<T>; void print_result(L* f, int m) { REP(i, m) { cout << f[i] << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k, m; cin >> n >> k >> m; V<V<pair<int, L>>> jel_of_col(k); REP(i, n) { int ki, mi; L ci; cin >> ki >> mi >> ci; jel_of_col[ki - 1].EB(mi, ci); } L f[m]; fill(f, f + m, -1); L cost0 = 0; int mass0 = 0; for (auto& jc : jel_of_col) { if (jc.empty()) { f[0] = 0; print_result(f, m); return 0; } cost0 += jc.front().second; mass0 += jc.front().first; } mass0 %= m; f[mass0] = cost0; for (auto& jc : jel_of_col) { L new_f[m]; copy(f, f + m, new_f); auto& [m0, c0] = jc.front(); for (auto& [mi, ci] : jc) { REP(j, m) { L f2 = f[(j - mi + m0 + m) % m]; if (new_f[j] == -1 && f2 != -1) { new_f[j] = f2 + ci - c0; } else if (new_f[j] != -1 && f2 != -1) { new_f[j] = min(new_f[j], f2 + ci - c0); } } } copy(new_f, new_f + m, f); } V<int> q; FOR(i, 1, m) { if (f[i] != -1) { q.EB(i); } } while (!q.empty()) { V<bool> improved(m); FOR(i, 1, m) { for (auto m1 : q) { L c2 = f[(i - m1 + m) % m]; if (c2 == -1) { continue; } L c1 = f[m1]; if (f[i] == -1) { f[i] = c1 + c2; improved[i] = true; } else if (f[i] > c1 + c2) { f[i] = c1 + c2; improved[i] = true; } } } q.clear(); FOR(i, 1, m) { if (improved[i]) { q.EB(i); } } } f[0] = 0; print_result(f, m); }
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 | #include <bits/stdc++.h> using namespace std; #define endl '\n' #define L long long #define MP make_pair #define REP(i, n) for(int i = 0; i < n; ++i) #define REPR(i, n) for(int i = n - 1; i >= 0; --i) #define FOR(i, a, b) for(int i = a; i < b; ++i) #define FORR(i, a, b) for(int i = b - 1; i >= a; --i) #define EB emplace_back #define ST first #define ND second #define S size #define RS resize template<class T> using P = pair<T, T>; template<class T> using V = vector<T>; void print_result(L* f, int m) { REP(i, m) { cout << f[i] << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k, m; cin >> n >> k >> m; V<V<pair<int, L>>> jel_of_col(k); REP(i, n) { int ki, mi; L ci; cin >> ki >> mi >> ci; jel_of_col[ki - 1].EB(mi, ci); } L f[m]; fill(f, f + m, -1); L cost0 = 0; int mass0 = 0; for (auto& jc : jel_of_col) { if (jc.empty()) { f[0] = 0; print_result(f, m); return 0; } cost0 += jc.front().second; mass0 += jc.front().first; } mass0 %= m; f[mass0] = cost0; for (auto& jc : jel_of_col) { L new_f[m]; copy(f, f + m, new_f); auto& [m0, c0] = jc.front(); for (auto& [mi, ci] : jc) { REP(j, m) { L f2 = f[(j - mi + m0 + m) % m]; if (new_f[j] == -1 && f2 != -1) { new_f[j] = f2 + ci - c0; } else if (new_f[j] != -1 && f2 != -1) { new_f[j] = min(new_f[j], f2 + ci - c0); } } } copy(new_f, new_f + m, f); } V<int> q; FOR(i, 1, m) { if (f[i] != -1) { q.EB(i); } } while (!q.empty()) { V<bool> improved(m); FOR(i, 1, m) { for (auto m1 : q) { L c2 = f[(i - m1 + m) % m]; if (c2 == -1) { continue; } L c1 = f[m1]; if (f[i] == -1) { f[i] = c1 + c2; improved[i] = true; } else if (f[i] > c1 + c2) { f[i] = c1 + c2; improved[i] = true; } } } q.clear(); FOR(i, 1, m) { if (improved[i]) { q.EB(i); } } } f[0] = 0; print_result(f, m); } |