#include <cstdio> #include <vector> #include <map> #include <algorithm> using namespace std; struct Sweet { unsigned long long k, m, c; Sweet(unsigned long long k, unsigned long long m, unsigned long long c) { this->k = k; this->m = m; this->c = c; } }; map<unsigned long long, unsigned long long> cached_first; void calculate_cache(map<unsigned long long, vector<Sweet*>>& color_map, unsigned long long m) { map<unsigned long long, unsigned long long> result_map; result_map[0] = 0; for (auto& color_data : color_map) { map<unsigned long long, unsigned long long> temp_map; // printf("%llu\n", color_data.first); for (auto& sweet : color_data.second) { for (auto& entry : result_map) { auto mass = entry.first; auto cost = entry.second; auto mass_remainder = (sweet->m + mass) % m; if (temp_map.contains(mass_remainder)) { temp_map[mass_remainder] = min( temp_map[mass_remainder], cost + sweet->c ); } else { temp_map[mass_remainder] = cost + sweet->c; } } } result_map = temp_map; } cached_first = result_map; } auto get_next_pack_result( map<unsigned long long, unsigned long long>& initial_map, map<unsigned long long, vector<Sweet*>>& color_map, unsigned long long m ) { map<unsigned long long, unsigned long long> result_map; for (auto& entry_initial : initial_map) { for (auto& entry : cached_first) { auto mass = entry.first; auto cost = entry.second; auto mass_remainder = (entry_initial.first + mass) % m; if (result_map.contains(mass_remainder)) { result_map[mass_remainder] = min( result_map[mass_remainder], cost + entry_initial.second ); } else { result_map[mass_remainder] = cost + entry_initial.second; } } } return result_map; } map<unsigned long long, unsigned long long> result; auto clean(map<unsigned long long, unsigned long long>& temp) { map<unsigned long long, unsigned long long> temp2; for (auto& i : temp) { if (result.contains(i.first)) { auto current = result[i.first]; if (current > i.second) { temp2[i.first] = i.second; result[i.first] = i.second; } } else { temp2[i.first] = i.second; result[i.first] = i.second; } } return temp2; } int main() { unsigned long long n, k, m; scanf("%llu %llu %llu", &n, &k, &m); vector<Sweet*> sweets(n); map<unsigned long long, vector<Sweet*>> color_sweets_map; for (unsigned long long i = 1; i <= k; i++) { color_sweets_map[i] = vector<Sweet*>(); } for (unsigned long long i = 0; i < n; i++) { unsigned long long ki, mi, ci; scanf("%llu %llu %llu", &ki, &mi, &ci); Sweet* new_sweet = new Sweet(ki, mi, ci); sweets[i] = new_sweet; color_sweets_map[ki].push_back(new_sweet); } calculate_cache(color_sweets_map, m); // for (auto a : color_sweets_map) { // printf("k: %d\n", a.first); // for (auto b : a.second) { // printf("%d %d %d\n", b->k, b->m, b->c); // } // } // for (auto i : cached_first) { // printf("m: %llu c: %llu\n", i.first, i.second); // } map<unsigned long long, unsigned long long> temp; result[0] = 0; temp[0] = 0; while (!temp.empty()) { temp = get_next_pack_result(temp, color_sweets_map, m); temp = clean(temp); // for (auto a : temp) { // printf("m: %llu c: %llu\n", a.first, a.second); // } } // puts("RESULT"); for (int i = 0; i < m; i++) { if (result.contains(i)) { printf("%llu\n", result[i]); } else { puts("-1"); } } }
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | #include <cstdio> #include <vector> #include <map> #include <algorithm> using namespace std; struct Sweet { unsigned long long k, m, c; Sweet(unsigned long long k, unsigned long long m, unsigned long long c) { this->k = k; this->m = m; this->c = c; } }; map<unsigned long long, unsigned long long> cached_first; void calculate_cache(map<unsigned long long, vector<Sweet*>>& color_map, unsigned long long m) { map<unsigned long long, unsigned long long> result_map; result_map[0] = 0; for (auto& color_data : color_map) { map<unsigned long long, unsigned long long> temp_map; // printf("%llu\n", color_data.first); for (auto& sweet : color_data.second) { for (auto& entry : result_map) { auto mass = entry.first; auto cost = entry.second; auto mass_remainder = (sweet->m + mass) % m; if (temp_map.contains(mass_remainder)) { temp_map[mass_remainder] = min( temp_map[mass_remainder], cost + sweet->c ); } else { temp_map[mass_remainder] = cost + sweet->c; } } } result_map = temp_map; } cached_first = result_map; } auto get_next_pack_result( map<unsigned long long, unsigned long long>& initial_map, map<unsigned long long, vector<Sweet*>>& color_map, unsigned long long m ) { map<unsigned long long, unsigned long long> result_map; for (auto& entry_initial : initial_map) { for (auto& entry : cached_first) { auto mass = entry.first; auto cost = entry.second; auto mass_remainder = (entry_initial.first + mass) % m; if (result_map.contains(mass_remainder)) { result_map[mass_remainder] = min( result_map[mass_remainder], cost + entry_initial.second ); } else { result_map[mass_remainder] = cost + entry_initial.second; } } } return result_map; } map<unsigned long long, unsigned long long> result; auto clean(map<unsigned long long, unsigned long long>& temp) { map<unsigned long long, unsigned long long> temp2; for (auto& i : temp) { if (result.contains(i.first)) { auto current = result[i.first]; if (current > i.second) { temp2[i.first] = i.second; result[i.first] = i.second; } } else { temp2[i.first] = i.second; result[i.first] = i.second; } } return temp2; } int main() { unsigned long long n, k, m; scanf("%llu %llu %llu", &n, &k, &m); vector<Sweet*> sweets(n); map<unsigned long long, vector<Sweet*>> color_sweets_map; for (unsigned long long i = 1; i <= k; i++) { color_sweets_map[i] = vector<Sweet*>(); } for (unsigned long long i = 0; i < n; i++) { unsigned long long ki, mi, ci; scanf("%llu %llu %llu", &ki, &mi, &ci); Sweet* new_sweet = new Sweet(ki, mi, ci); sweets[i] = new_sweet; color_sweets_map[ki].push_back(new_sweet); } calculate_cache(color_sweets_map, m); // for (auto a : color_sweets_map) { // printf("k: %d\n", a.first); // for (auto b : a.second) { // printf("%d %d %d\n", b->k, b->m, b->c); // } // } // for (auto i : cached_first) { // printf("m: %llu c: %llu\n", i.first, i.second); // } map<unsigned long long, unsigned long long> temp; result[0] = 0; temp[0] = 0; while (!temp.empty()) { temp = get_next_pack_result(temp, color_sweets_map, m); temp = clean(temp); // for (auto a : temp) { // printf("m: %llu c: %llu\n", a.first, a.second); // } } // puts("RESULT"); for (int i = 0; i < m; i++) { if (result.contains(i)) { printf("%llu\n", result[i]); } else { puts("-1"); } } } |