#include <iostream> #include <vector> using namespace std; struct Z { int m; int c; Z(int m, int c): m(m), c(c) { } }; vector<vector<Z>> bucket; int n,k,m; int main() { cin>>n>>k>>m; for(int i = 0 ; i < k+5 ; i ++) { bucket.emplace_back(); } for(int i = 0 ; i < n ; i ++) { int k,masa,c; cin>>k>>masa>>c; bucket[k].emplace_back(masa % m, c); } vector<long long> dp(m, -1); for(int i = 0 ; i < bucket[1].size() ; i ++) { auto cuks = bucket[1][i]; if( dp[ cuks.m ] == -1 || dp[ cuks.m ] > cuks.c ) { dp[ cuks.m ] = cuks.c; } } for(int i = 2; i <= k ; i ++) { vector<long long> tmp(m, -1); for(int j = 0 ; j < dp.size() ; j ++) { if( dp[j] == -1 ) continue; for(auto cuks : bucket[i]) { int new_m = ( j + cuks.m )%m; long long new_c = dp[j] + cuks.c; if( tmp[new_m] == -1 || tmp[new_m] > new_c) { tmp[new_m] = new_c; } } } dp = tmp; } dp[0] = 0; auto dp2(dp); bool flag = true; while( flag ) { flag = false; vector<long long> tmp(dp2); for(int i = 0 ; i < tmp.size() ; i ++) { if( tmp[i] == -1) continue; for(int j = 0 ; j < dp2.size() ; j++) { if( dp2[j] == -1) continue; int new_m = ( i + j ) % m; long long new_c = ( tmp[i] + dp2[j] ); if( tmp[new_m] == -1 || tmp[new_m] > new_c) { flag = true; tmp[new_m] = new_c; } } } dp2 = tmp; } for(int i = 0 ; i < dp2.size() ; i ++) { cout<<dp2[i]<<endl; } }
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 110 111 | #include <iostream> #include <vector> using namespace std; struct Z { int m; int c; Z(int m, int c): m(m), c(c) { } }; vector<vector<Z>> bucket; int n,k,m; int main() { cin>>n>>k>>m; for(int i = 0 ; i < k+5 ; i ++) { bucket.emplace_back(); } for(int i = 0 ; i < n ; i ++) { int k,masa,c; cin>>k>>masa>>c; bucket[k].emplace_back(masa % m, c); } vector<long long> dp(m, -1); for(int i = 0 ; i < bucket[1].size() ; i ++) { auto cuks = bucket[1][i]; if( dp[ cuks.m ] == -1 || dp[ cuks.m ] > cuks.c ) { dp[ cuks.m ] = cuks.c; } } for(int i = 2; i <= k ; i ++) { vector<long long> tmp(m, -1); for(int j = 0 ; j < dp.size() ; j ++) { if( dp[j] == -1 ) continue; for(auto cuks : bucket[i]) { int new_m = ( j + cuks.m )%m; long long new_c = dp[j] + cuks.c; if( tmp[new_m] == -1 || tmp[new_m] > new_c) { tmp[new_m] = new_c; } } } dp = tmp; } dp[0] = 0; auto dp2(dp); bool flag = true; while( flag ) { flag = false; vector<long long> tmp(dp2); for(int i = 0 ; i < tmp.size() ; i ++) { if( tmp[i] == -1) continue; for(int j = 0 ; j < dp2.size() ; j++) { if( dp2[j] == -1) continue; int new_m = ( i + j ) % m; long long new_c = ( tmp[i] + dp2[j] ); if( tmp[new_m] == -1 || tmp[new_m] > new_c) { flag = true; tmp[new_m] = new_c; } } } dp2 = tmp; } for(int i = 0 ; i < dp2.size() ; i ++) { cout<<dp2[i]<<endl; } } |