#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1e18; const int MAX = 1e3 * 7 + 10; ll dp[2][MAX]; ll best[MAX]; struct zel{ int k, m, id; ll w; void read(int i){ cin >> k >> m >> w; id = i; } }; bool cmp(const zel &a, const zel &b){ if (a.k == b.k){ return a.id < b.id; } return a.k < b.k; } bool K[MAX], visited[MAX]; zel Z[MAX]; vector<pair<ll,ll>>Z1; void solve(int n, int m, int k){ for (int i = 1; i <= k; i++){ if (!K[i]){ for (int x = 0; x < m; x++){ cout << (x == 0 ? 0 : -1) << "\n"; } return; } } sort(Z,Z+n,cmp); for (int i = 0; i < 2; i++){ fill(dp[i],dp[i]+m,INF); } fill(best,best+m,INF); int cur = 0, last = 1; bool fi = true; for (int i = 0; i < n; i++){ //O(n) //cout << Z[i].k << " " << Z[i].m << " " << Z[i].w << endl; if (i > 0 && Z[i].k != Z[i-1].k){ // for (int x = 0; x < m; x++){ // cout << dp[cur][x] << " "; // } // cout << endl << endl; cur ^= 1; last = cur ^ 1; fill(dp[cur],dp[cur]+m,INF); fi = false; } if (fi){ dp[cur][Z[i].m >= m ? Z[i].m - m : Z[i].m] = min(dp[cur][Z[i].m >= m ? Z[i].m - m : Z[i].m],Z[i].w); } else { for (int x = 0; x < m; x++){ //O(n^2) dp[cur][(x + Z[i].m >= m ? x + Z[i].m - m : x + Z[i].m)] = min(dp[cur][(x + Z[i].m >= m ? x + Z[i].m - m : x + Z[i].m)], dp[last][x] + Z[i].w); } } } for (int j = 0; j < 2; j++){ for (int i = 1; i < m; i++){ ll val = dp[cur][i], curval = dp[cur][i]; int pos = i; while (!visited[pos]){ visited[pos] = true; dp[cur][pos] = min(dp[cur][pos], curval); pos += i; if (pos >= m){ pos -= m; } curval = min(curval + val, INF); } fill(visited,visited+m,false); } } for (int i = 1; i < m; i++){ if (dp[cur][i] < INF){ Z1.push_back({i, dp[cur][i]}); } } best[0] = 0; for (int rep = 0; rep < 5; rep++){ for (int i = 0; i < m; i++){ for (pair<ll,ll> &tmp: Z1){ //O(rep * m^2) best[i] = min(tmp.second + best[i - tmp.first < 0 ? i-tmp.first+m : i-tmp.first], best[i]); } } } for (int i = 0; i < m; i++){ cout << (best[i] >= INF ? -1 : best[i]) << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, k, m; cin >> n >> k >> m; for (int i = 0; i < n; i++){ Z[i].read(i); K[Z[i].k] = true; } solve(n,m,k); }
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 106 107 108 109 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1e18; const int MAX = 1e3 * 7 + 10; ll dp[2][MAX]; ll best[MAX]; struct zel{ int k, m, id; ll w; void read(int i){ cin >> k >> m >> w; id = i; } }; bool cmp(const zel &a, const zel &b){ if (a.k == b.k){ return a.id < b.id; } return a.k < b.k; } bool K[MAX], visited[MAX]; zel Z[MAX]; vector<pair<ll,ll>>Z1; void solve(int n, int m, int k){ for (int i = 1; i <= k; i++){ if (!K[i]){ for (int x = 0; x < m; x++){ cout << (x == 0 ? 0 : -1) << "\n"; } return; } } sort(Z,Z+n,cmp); for (int i = 0; i < 2; i++){ fill(dp[i],dp[i]+m,INF); } fill(best,best+m,INF); int cur = 0, last = 1; bool fi = true; for (int i = 0; i < n; i++){ //O(n) //cout << Z[i].k << " " << Z[i].m << " " << Z[i].w << endl; if (i > 0 && Z[i].k != Z[i-1].k){ // for (int x = 0; x < m; x++){ // cout << dp[cur][x] << " "; // } // cout << endl << endl; cur ^= 1; last = cur ^ 1; fill(dp[cur],dp[cur]+m,INF); fi = false; } if (fi){ dp[cur][Z[i].m >= m ? Z[i].m - m : Z[i].m] = min(dp[cur][Z[i].m >= m ? Z[i].m - m : Z[i].m],Z[i].w); } else { for (int x = 0; x < m; x++){ //O(n^2) dp[cur][(x + Z[i].m >= m ? x + Z[i].m - m : x + Z[i].m)] = min(dp[cur][(x + Z[i].m >= m ? x + Z[i].m - m : x + Z[i].m)], dp[last][x] + Z[i].w); } } } for (int j = 0; j < 2; j++){ for (int i = 1; i < m; i++){ ll val = dp[cur][i], curval = dp[cur][i]; int pos = i; while (!visited[pos]){ visited[pos] = true; dp[cur][pos] = min(dp[cur][pos], curval); pos += i; if (pos >= m){ pos -= m; } curval = min(curval + val, INF); } fill(visited,visited+m,false); } } for (int i = 1; i < m; i++){ if (dp[cur][i] < INF){ Z1.push_back({i, dp[cur][i]}); } } best[0] = 0; for (int rep = 0; rep < 5; rep++){ for (int i = 0; i < m; i++){ for (pair<ll,ll> &tmp: Z1){ //O(rep * m^2) best[i] = min(tmp.second + best[i - tmp.first < 0 ? i-tmp.first+m : i-tmp.first], best[i]); } } } for (int i = 0; i < m; i++){ cout << (best[i] >= INF ? -1 : best[i]) << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, k, m; cin >> n >> k >> m; for (int i = 0; i < n; i++){ Z[i].read(i); K[Z[i].k] = true; } solve(n,m,k); } |