#include <iostream> #include <vector> #include <algorithm> using namespace std; const long long INF = 1e18; int main() { int n, k, m; cin >> n >> k >> m; vector<vector<long long>> dp(m, vector<long long>(k + 1, INF)); dp[0][0] = 0; for (int i = 0; i < n; i++) { int color, weight, cost; cin >> color >> weight >> cost; for (int j = 0; j < m; j++) { if (dp[j][color - 1] != INF) { int newWeight = (j + weight) % m; dp[newWeight][color] = min(dp[newWeight][color], dp[j][color - 1] + cost); } } } for (int r = 0; r < m; r++) { long long minCost = INF; for (int color = 1; color <= k; color++) { minCost = min(minCost, dp[r][color]); } if (minCost == INF) { cout << -1 << endl; } else { cout << minCost << endl; } } 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; const long long INF = 1e18; int main() { int n, k, m; cin >> n >> k >> m; vector<vector<long long>> dp(m, vector<long long>(k + 1, INF)); dp[0][0] = 0; for (int i = 0; i < n; i++) { int color, weight, cost; cin >> color >> weight >> cost; for (int j = 0; j < m; j++) { if (dp[j][color - 1] != INF) { int newWeight = (j + weight) % m; dp[newWeight][color] = min(dp[newWeight][color], dp[j][color - 1] + cost); } } } for (int r = 0; r < m; r++) { long long minCost = INF; for (int color = 1; color <= k; color++) { minCost = min(minCost, dp[r][color]); } if (minCost == INF) { cout << -1 << endl; } else { cout << minCost << endl; } } return 0; } |