#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair <int, int>; using pll = pair <ll, ll>; using pil = pair <int, ll>; const int inf = 1e9+7; const ll inf_ll = 1e18+7; void boost () { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int n, k, m, kolor, masa; ll koszt, mini[7007], pom[7007], res[7007]; vector<pil> zelki[7007], v; int main () { boost(); cin >> n >> k >> m; for (int i = 1; i <= n; i++) { cin >> kolor >> masa >> koszt; zelki[kolor].push_back({masa, koszt}); } mini[0] = res[0] = 0; for (int i = 1; i < m; i++) mini[i] = res[i] = inf_ll; for (int kolor = 1; kolor <= k; kolor++) { if (zelki[kolor].size() == 0) { cout << "0\n"; for (int i = 1; i < m; i++) cout << "-1\n"; return 0; } for (int i = 0; i < m; i++) pom[i] = inf_ll; for (pil p: zelki[kolor]) { for (int i = 0; i < m; i++) { int id = (p.first + i) % m; pom[id] = min(pom[id], mini[i] + p.second); } } for (int i = 0; i < m; i++) mini[i] = pom[i]; } for (int i = 0; i < m; i++) { //cout << "mini " << i << " = " << mini[i] << "\n"; if (mini[i] != inf_ll) v.push_back({i, mini[i]}); } for (pil p: v) { int masa = p.first; ll koszt = p.second; int x = masa; ll suma = koszt; while (x != 0) { mini[x] = min(mini[x], suma); suma += koszt; x = (x + masa) % m; } } for (int i = 0; i < m; i++) { if (mini[i] == inf_ll) continue; for (int j = 0; j < m; j++) { int y = (j + i) % m; res[y] = min(res[y], res[j] + mini[i]); } } for (int i = 0; i < m; i++) { cout << ((res[i] == inf_ll) ? -1 : 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair <int, int>; using pll = pair <ll, ll>; using pil = pair <int, ll>; const int inf = 1e9+7; const ll inf_ll = 1e18+7; void boost () { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int n, k, m, kolor, masa; ll koszt, mini[7007], pom[7007], res[7007]; vector<pil> zelki[7007], v; int main () { boost(); cin >> n >> k >> m; for (int i = 1; i <= n; i++) { cin >> kolor >> masa >> koszt; zelki[kolor].push_back({masa, koszt}); } mini[0] = res[0] = 0; for (int i = 1; i < m; i++) mini[i] = res[i] = inf_ll; for (int kolor = 1; kolor <= k; kolor++) { if (zelki[kolor].size() == 0) { cout << "0\n"; for (int i = 1; i < m; i++) cout << "-1\n"; return 0; } for (int i = 0; i < m; i++) pom[i] = inf_ll; for (pil p: zelki[kolor]) { for (int i = 0; i < m; i++) { int id = (p.first + i) % m; pom[id] = min(pom[id], mini[i] + p.second); } } for (int i = 0; i < m; i++) mini[i] = pom[i]; } for (int i = 0; i < m; i++) { //cout << "mini " << i << " = " << mini[i] << "\n"; if (mini[i] != inf_ll) v.push_back({i, mini[i]}); } for (pil p: v) { int masa = p.first; ll koszt = p.second; int x = masa; ll suma = koszt; while (x != 0) { mini[x] = min(mini[x], suma); suma += koszt; x = (x + masa) % m; } } for (int i = 0; i < m; i++) { if (mini[i] == inf_ll) continue; for (int j = 0; j < m; j++) { int y = (j + i) % m; res[y] = min(res[y], res[j] + mini[i]); } } for (int i = 0; i < m; i++) { cout << ((res[i] == inf_ll) ? -1 : res[i]) << "\n"; } return 0; } |