#include <iostream> #include <algorithm> #include <map> #include <vector> #include <stack> #include <climits> using namespace std; struct Jelly { int mass; int cost; }; vector<vector<int>> dp(7005, vector<int>(7005, -1)); vector<vector<int>> dppre(7005, vector<int>(7005, -1)); int mod(int a, int b) { int r = a % b; return r >= 0 ? r : r + std::abs(b); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n, k, m; cin >> n >> k >> m; // color, mass -> (min) cost map<pair<int, int>, int> v; int ki, mi, ci; for (int i = 0; i < n; ++i) { cin >> ki >> mi >> ci; if (v.find({ki, mi}) == v.end()) { v[{ki, mi}] = ci; } else { v[{ki, mi}] = min(v[{ki, mi}], ci); } } // dppre for ki == 1 dppre[1][0] = 0; for (int mmm = 1; mmm < m; ++mmm) { if (v.find({1, mmm}) != v.end()) { dppre[1][mmm] = v[{1, mmm}]; } } for (int ki = 2; ki <= k; ++ki) { for (int mi = 0; mi < m; ++mi) { if (v.find({ki, mi}) == v.end()) { continue; } for (int mmm = 0; mmm < m; ++mmm) { if (dppre[ki - 1][mmm] == -1) { continue; } if (dppre[ki][mod(mmm + mi, m)] == -1) { dppre[ki][mod(mmm + mi, m)] = v[{ki, mi}] + dppre[ki - 1][mmm]; } else { dppre[ki][mod(mmm + mi, m)] = min(dppre[ki][mod(mmm + mi, m)], v[{ki, mi}] + dppre[ki - 1][mmm]); } } } } // for izk = 1 dp[1][0] = 0; for (int mmm = 1; mmm < m; ++mmm) { dp[1][mmm] = dppre[k][mmm]; } for (int izk = 2; izk < m; ++izk) { for (int mmm = 0; mmm < m; ++mmm) { if (dp[izk - 1][mmm] != -1) { dp[izk][mmm] = dp[izk - 1][mmm]; continue; } int r = INT_MAX; for (int i = 1; i < m; ++i) { if (dp[izk - 1][i] != -1 && dp[izk - 1][mod(mmm - i, m)] != -1) { r = min(r, dp[izk - 1][i] + dp[izk - 1][mod(mmm - i, m)]); } } if (r != INT_MAX) { dp[izk][mmm] = r; } } } for (int mmm = 0; mmm < m; ++mmm) { cout << dp[m - 1][mmm] << "\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 | #include <iostream> #include <algorithm> #include <map> #include <vector> #include <stack> #include <climits> using namespace std; struct Jelly { int mass; int cost; }; vector<vector<int>> dp(7005, vector<int>(7005, -1)); vector<vector<int>> dppre(7005, vector<int>(7005, -1)); int mod(int a, int b) { int r = a % b; return r >= 0 ? r : r + std::abs(b); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n, k, m; cin >> n >> k >> m; // color, mass -> (min) cost map<pair<int, int>, int> v; int ki, mi, ci; for (int i = 0; i < n; ++i) { cin >> ki >> mi >> ci; if (v.find({ki, mi}) == v.end()) { v[{ki, mi}] = ci; } else { v[{ki, mi}] = min(v[{ki, mi}], ci); } } // dppre for ki == 1 dppre[1][0] = 0; for (int mmm = 1; mmm < m; ++mmm) { if (v.find({1, mmm}) != v.end()) { dppre[1][mmm] = v[{1, mmm}]; } } for (int ki = 2; ki <= k; ++ki) { for (int mi = 0; mi < m; ++mi) { if (v.find({ki, mi}) == v.end()) { continue; } for (int mmm = 0; mmm < m; ++mmm) { if (dppre[ki - 1][mmm] == -1) { continue; } if (dppre[ki][mod(mmm + mi, m)] == -1) { dppre[ki][mod(mmm + mi, m)] = v[{ki, mi}] + dppre[ki - 1][mmm]; } else { dppre[ki][mod(mmm + mi, m)] = min(dppre[ki][mod(mmm + mi, m)], v[{ki, mi}] + dppre[ki - 1][mmm]); } } } } // for izk = 1 dp[1][0] = 0; for (int mmm = 1; mmm < m; ++mmm) { dp[1][mmm] = dppre[k][mmm]; } for (int izk = 2; izk < m; ++izk) { for (int mmm = 0; mmm < m; ++mmm) { if (dp[izk - 1][mmm] != -1) { dp[izk][mmm] = dp[izk - 1][mmm]; continue; } int r = INT_MAX; for (int i = 1; i < m; ++i) { if (dp[izk - 1][i] != -1 && dp[izk - 1][mod(mmm - i, m)] != -1) { r = min(r, dp[izk - 1][i] + dp[izk - 1][mod(mmm - i, m)]); } } if (r != INT_MAX) { dp[izk][mmm] = r; } } } for (int mmm = 0; mmm < m; ++mmm) { cout << dp[m - 1][mmm] << "\n"; } return 0; } |