#include <algorithm> #include <iostream> #include <limits> #include <vector> using namespace std; #define ULL_MAX numeric_limits<ull>::max() using ull = unsigned long long; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k, m; cin >> n >> k >> m; vector<vector<pair<ull, int>>> z(k); // z[kolor][i] = {cena, masa} for (int i = 0; i < n; i++) { int k, c, m; cin >> k >> m >> c; z[k - 1].push_back({c, m}); } vector<ull> actPrices(m, ULL_MAX); vector<ull> prevPrices(m, ULL_MAX); prevPrices[0] = 0; for (int curk = 0; curk < k; curk++) { for (auto &[cena, masa] : z[curk]) { for (int i = 0; i < m; i++) { if (prevPrices[i] == ULL_MAX) { continue; } actPrices[(i + masa) % m] = min(prevPrices[i] + cena, actPrices[(i + masa) % m]); } } prevPrices = actPrices; fill(actPrices.begin(), actPrices.end(), ULL_MAX); } vector<ull> res(m, ULL_MAX); res[0] = 0; for (int jmp = 1; jmp < m; jmp++) { if (prevPrices[jmp] == ULL_MAX) { continue; } auto cena = prevPrices[jmp]; vector<int> was(m, 0); for (int i = 0; i < m; i++) { if (res[i] == ULL_MAX) { continue; } int j = i; while (was[j] < 2) { was[j]++; int target = (j + jmp) % m; res[target] = min(res[j] + cena, res[target]); j = target; } } } for (int i = 0; i < m; i++) { if (res[i] == ULL_MAX) { cout << -1 << "\n"; } else { cout << res[i] << "\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 | #include <algorithm> #include <iostream> #include <limits> #include <vector> using namespace std; #define ULL_MAX numeric_limits<ull>::max() using ull = unsigned long long; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k, m; cin >> n >> k >> m; vector<vector<pair<ull, int>>> z(k); // z[kolor][i] = {cena, masa} for (int i = 0; i < n; i++) { int k, c, m; cin >> k >> m >> c; z[k - 1].push_back({c, m}); } vector<ull> actPrices(m, ULL_MAX); vector<ull> prevPrices(m, ULL_MAX); prevPrices[0] = 0; for (int curk = 0; curk < k; curk++) { for (auto &[cena, masa] : z[curk]) { for (int i = 0; i < m; i++) { if (prevPrices[i] == ULL_MAX) { continue; } actPrices[(i + masa) % m] = min(prevPrices[i] + cena, actPrices[(i + masa) % m]); } } prevPrices = actPrices; fill(actPrices.begin(), actPrices.end(), ULL_MAX); } vector<ull> res(m, ULL_MAX); res[0] = 0; for (int jmp = 1; jmp < m; jmp++) { if (prevPrices[jmp] == ULL_MAX) { continue; } auto cena = prevPrices[jmp]; vector<int> was(m, 0); for (int i = 0; i < m; i++) { if (res[i] == ULL_MAX) { continue; } int j = i; while (was[j] < 2) { was[j]++; int target = (j + jmp) % m; res[target] = min(res[j] + cena, res[target]); j = target; } } } for (int i = 0; i < m; i++) { if (res[i] == ULL_MAX) { cout << -1 << "\n"; } else { cout << res[i] << "\n"; } } return 0; } |