#include<bits/stdc++.h> using namespace std; using lld = long long; const lld INF = 1e18; struct Jelly { int color; int weight; int cost; }; bool cmp_by_color(const Jelly& a, const Jelly& b) { return a.color < b.color; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,k,m; cin >> n >> k >> m; vector<Jelly> t(n); for(auto& i : t) cin >> i.color >> i.weight >> i.cost; sort(t.begin(), t.end(), cmp_by_color); vector<lld> p; { vector<vector<lld>> dp(2, vector<lld>(m, INF)); int it = 0; dp[1][0] = 0; int last = 0; for(auto& i : t) { if(i.color != last) { it = 1-it; fill(dp[1-it].begin(), dp[1-it].end(), INF); if(i.color != last+1) break; last = i.color; } for(int j=0;j<m;j++) dp[1-it][(j+i.weight) % m] = min(dp[1-it][(j+i.weight) % m], dp[it][j] + i.cost); } p = dp[1-it]; } vector<lld> result(m, INF); result[0] = 0; { vector<bool> vis(m, false); for(int it=0;it<m;it++) { int v = -1; for(int i=0;i<m;i++) if(!vis[i] && (v == -1 || result[i] < result[v])) v = i; vis[v] = true; for(int i=0;i<m;i++) result[(v+i) % m] = min(result[(v+i) % m], result[v] + p[i]); } } for(auto i : result) cout << (i != INF ? i : -1) << "\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 | #include<bits/stdc++.h> using namespace std; using lld = long long; const lld INF = 1e18; struct Jelly { int color; int weight; int cost; }; bool cmp_by_color(const Jelly& a, const Jelly& b) { return a.color < b.color; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,k,m; cin >> n >> k >> m; vector<Jelly> t(n); for(auto& i : t) cin >> i.color >> i.weight >> i.cost; sort(t.begin(), t.end(), cmp_by_color); vector<lld> p; { vector<vector<lld>> dp(2, vector<lld>(m, INF)); int it = 0; dp[1][0] = 0; int last = 0; for(auto& i : t) { if(i.color != last) { it = 1-it; fill(dp[1-it].begin(), dp[1-it].end(), INF); if(i.color != last+1) break; last = i.color; } for(int j=0;j<m;j++) dp[1-it][(j+i.weight) % m] = min(dp[1-it][(j+i.weight) % m], dp[it][j] + i.cost); } p = dp[1-it]; } vector<lld> result(m, INF); result[0] = 0; { vector<bool> vis(m, false); for(int it=0;it<m;it++) { int v = -1; for(int i=0;i<m;i++) if(!vis[i] && (v == -1 || result[i] < result[v])) v = i; vis[v] = true; for(int i=0;i<m;i++) result[(v+i) % m] = min(result[(v+i) % m], result[v] + p[i]); } } for(auto i : result) cout << (i != INF ? i : -1) << "\n"; return 0; } |