#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define mid ((l+r)/2) #define siz(x) (int)(x).size() #define BOOST ios_base::sync_with_stdio(0), cin.tie(0) #define deb(x) cout << #x << ": " << x << "\n" typedef long long ll; typedef long double ld; typedef pair<int, int> ii; struct jelly{ int type; int mass; int cost; bool operator<(jelly x){ return type < x.type; } }; const int N = 7005; bool vis[N]; ll best[N]; ll nxt[N]; ll d[N]; int main(){ BOOST; int n, k, m; cin >> n >> k >> m; vector<jelly> t(n); for(int i=0; i<n; i++){ cin >> t[i].type >> t[i].mass >> t[i].cost; } sort(all(t)); for(int i=0; i<m; i++) best[i] = -1, nxt[i] = -1; best[0] = 0; for(int i=0, col=1; col<=k; col++){ while(i < n && t[i].type == col){ for(int j=0; j<m; j++){ if(best[j] != -1){ int go = (j + t[i].mass)%m; if(nxt[go] == -1 || best[j] + t[i].cost < nxt[go]){ nxt[go] = best[j] + t[i].cost; } } } i++; } for(int i=0; i<m; i++){ best[i] = nxt[i]; nxt[i] = -1; } } for(int i=0; i<m; i++) d[i] = -1; d[0] = 0; for(int j=0; j<m; j++){ int x = -1; for(int i=0; i<m; i++){ if(!vis[i] && d[i] != -1){ if(x == -1 || d[i] < d[x]){ x = i; } } } if(x == -1) break; vis[x] = 1; for(int i=0; i<m; i++){ if(best[i] == -1) continue; int go = x + i; if(go >= m) go -= m; if(d[go] == -1 || d[x] + best[i] < d[go]){ d[go] = d[x] + best[i]; } } } for(int i=0; i<m; i++){ cout << d[i] << "\n"; } }
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 | #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define mid ((l+r)/2) #define siz(x) (int)(x).size() #define BOOST ios_base::sync_with_stdio(0), cin.tie(0) #define deb(x) cout << #x << ": " << x << "\n" typedef long long ll; typedef long double ld; typedef pair<int, int> ii; struct jelly{ int type; int mass; int cost; bool operator<(jelly x){ return type < x.type; } }; const int N = 7005; bool vis[N]; ll best[N]; ll nxt[N]; ll d[N]; int main(){ BOOST; int n, k, m; cin >> n >> k >> m; vector<jelly> t(n); for(int i=0; i<n; i++){ cin >> t[i].type >> t[i].mass >> t[i].cost; } sort(all(t)); for(int i=0; i<m; i++) best[i] = -1, nxt[i] = -1; best[0] = 0; for(int i=0, col=1; col<=k; col++){ while(i < n && t[i].type == col){ for(int j=0; j<m; j++){ if(best[j] != -1){ int go = (j + t[i].mass)%m; if(nxt[go] == -1 || best[j] + t[i].cost < nxt[go]){ nxt[go] = best[j] + t[i].cost; } } } i++; } for(int i=0; i<m; i++){ best[i] = nxt[i]; nxt[i] = -1; } } for(int i=0; i<m; i++) d[i] = -1; d[0] = 0; for(int j=0; j<m; j++){ int x = -1; for(int i=0; i<m; i++){ if(!vis[i] && d[i] != -1){ if(x == -1 || d[i] < d[x]){ x = i; } } } if(x == -1) break; vis[x] = 1; for(int i=0; i<m; i++){ if(best[i] == -1) continue; int go = x + i; if(go >= m) go -= m; if(d[go] == -1 || d[x] + best[i] < d[go]){ d[go] = d[x] + best[i]; } } } for(int i=0; i<m; i++){ cout << d[i] << "\n"; } } |