#include <bits/stdc++.h> using namespace std; vector<vector<pair<long long, long long>>> colors; //first is price second is weight int main(){ long long n, k, m; cin>>n>>k>>m; colors.assign(k, {}); long long a, b, c; for(long long i = 0; i<n; i++){ cin>>a>>b>>c; a--; colors[a].push_back({c, b}); } vector<long long> bck, bck2; bck.assign(m, -1); bck[0] = 0; for(long long i = 0; i<k; i++){ bck2.assign(m, -1); for(long long j = 0; j<colors[i].size(); j++){ for(long long o = 0; o<m; o++){ if(bck[o]==-1) continue; long long nn = o + colors[i][j].second; nn %= m; if(bck2[nn]==-1) bck2[nn] = LLONG_MAX; bck2[nn] = min(bck2[nn], bck[o] + colors[i][j].first); } } bck = vector<long long>(bck2); } bck[0] = 0; for(long long i = 0; i<m; i++){ if(bck[i]==-1) continue; bck2 = vector<long long> (bck); for(long long j = 1; (j/2)*(j/2)<=2*m; j*=2){ bool chd = false; long long lv; for(long long o = 0; o<m; o++){ if(bck2[o]==-1) continue; long long nn = o + i * j; nn %= m; lv = bck2[nn]; long long val = bck2[o] + bck2[i] * j; if(bck2[nn]==-1) bck2[nn] = 1e18; bck2[nn] = min(bck2[nn], val); if(bck2[nn] != lv) chd = true; } if(!chd) break; } bck = vector<long long> (bck2); } for(long long val : bck){ cout<<val<<endl; } 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 | #include <bits/stdc++.h> using namespace std; vector<vector<pair<long long, long long>>> colors; //first is price second is weight int main(){ long long n, k, m; cin>>n>>k>>m; colors.assign(k, {}); long long a, b, c; for(long long i = 0; i<n; i++){ cin>>a>>b>>c; a--; colors[a].push_back({c, b}); } vector<long long> bck, bck2; bck.assign(m, -1); bck[0] = 0; for(long long i = 0; i<k; i++){ bck2.assign(m, -1); for(long long j = 0; j<colors[i].size(); j++){ for(long long o = 0; o<m; o++){ if(bck[o]==-1) continue; long long nn = o + colors[i][j].second; nn %= m; if(bck2[nn]==-1) bck2[nn] = LLONG_MAX; bck2[nn] = min(bck2[nn], bck[o] + colors[i][j].first); } } bck = vector<long long>(bck2); } bck[0] = 0; for(long long i = 0; i<m; i++){ if(bck[i]==-1) continue; bck2 = vector<long long> (bck); for(long long j = 1; (j/2)*(j/2)<=2*m; j*=2){ bool chd = false; long long lv; for(long long o = 0; o<m; o++){ if(bck2[o]==-1) continue; long long nn = o + i * j; nn %= m; lv = bck2[nn]; long long val = bck2[o] + bck2[i] * j; if(bck2[nn]==-1) bck2[nn] = 1e18; bck2[nn] = min(bck2[nn], val); if(bck2[nn] != lv) chd = true; } if(!chd) break; } bck = vector<long long> (bck2); } for(long long val : bck){ cout<<val<<endl; } return 0; } |