#include <bits/stdc++.h> using namespace std; const int MAXN = 7005; long long minPrices[MAXN][MAXN]; long long sumPrices[MAXN][MAXN]; void printNegative(int m) { cout << "0" << endl; for (int i = 1; i < m; i++) { cout << "-1" << endl; } } int main() { int n, k, m, col, wei, pri; cin >> n >> k >> m; set<int> colours; vector<int> weights[k + 1]; vector<int> sumWeights[k + 1] = {}; vector<int> minWeights = {}; for (int i = 1; i <= n; i++) { cin >> col >> wei >> pri; colours.insert(col); wei %= m; if (minPrices[col][wei] == 0) { weights[col].push_back(wei); minPrices[col][wei] = pri; continue; } if (minPrices[col][wei] > pri) { minPrices[col][wei] = pri; } } if (colours.size() < k) { printNegative(m); return 0; } sumWeights[1] = weights[1]; for (int wi : sumWeights[1]) { sumPrices[1][wi] = minPrices[1][wi]; } for (int i = 1; i < k; i++) { for (int wi : sumWeights[i]) { for (int wj : weights[i + 1]) { int sumw = (wi + wj) % m; long long value = sumPrices[i][wi] + minPrices[i + 1][wj]; if (sumPrices[i + 1][sumw] > value) { sumPrices[i + 1][sumw] = value; continue; } if (sumPrices[i + 1][sumw] == 0) { sumWeights[i + 1].push_back(sumw); sumPrices[i + 1][sumw] = value; } } } } minWeights = sumWeights[k]; while (sumWeights[k].size()) { sumWeights[1] = sumWeights[k]; sumWeights[k].clear(); for (int wi : sumWeights[1]) { for (int wj : minWeights) { int sumw = (wi + wj) % m; long long value = sumPrices[k][wi] + sumPrices[k][wj]; if (sumPrices[k][sumw] == 0 || sumPrices[k][sumw] > value) { sumWeights[k].push_back(sumw); sumPrices[k][sumw] = value; } } } } cout << "0" << endl; for (int i = 1; i < m; i++) { cout << (sumPrices[k][i] == 0 ? -1 : sumPrices[k][i]) << endl; } }
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 | #include <bits/stdc++.h> using namespace std; const int MAXN = 7005; long long minPrices[MAXN][MAXN]; long long sumPrices[MAXN][MAXN]; void printNegative(int m) { cout << "0" << endl; for (int i = 1; i < m; i++) { cout << "-1" << endl; } } int main() { int n, k, m, col, wei, pri; cin >> n >> k >> m; set<int> colours; vector<int> weights[k + 1]; vector<int> sumWeights[k + 1] = {}; vector<int> minWeights = {}; for (int i = 1; i <= n; i++) { cin >> col >> wei >> pri; colours.insert(col); wei %= m; if (minPrices[col][wei] == 0) { weights[col].push_back(wei); minPrices[col][wei] = pri; continue; } if (minPrices[col][wei] > pri) { minPrices[col][wei] = pri; } } if (colours.size() < k) { printNegative(m); return 0; } sumWeights[1] = weights[1]; for (int wi : sumWeights[1]) { sumPrices[1][wi] = minPrices[1][wi]; } for (int i = 1; i < k; i++) { for (int wi : sumWeights[i]) { for (int wj : weights[i + 1]) { int sumw = (wi + wj) % m; long long value = sumPrices[i][wi] + minPrices[i + 1][wj]; if (sumPrices[i + 1][sumw] > value) { sumPrices[i + 1][sumw] = value; continue; } if (sumPrices[i + 1][sumw] == 0) { sumWeights[i + 1].push_back(sumw); sumPrices[i + 1][sumw] = value; } } } } minWeights = sumWeights[k]; while (sumWeights[k].size()) { sumWeights[1] = sumWeights[k]; sumWeights[k].clear(); for (int wi : sumWeights[1]) { for (int wj : minWeights) { int sumw = (wi + wj) % m; long long value = sumPrices[k][wi] + sumPrices[k][wj]; if (sumPrices[k][sumw] == 0 || sumPrices[k][sumw] > value) { sumWeights[k].push_back(sumw); sumPrices[k][sumw] = value; } } } } cout << "0" << endl; for (int i = 1; i < m; i++) { cout << (sumPrices[k][i] == 0 ? -1 : sumPrices[k][i]) << endl; } } |