#include <iostream> #include <vector> #include <array> #include <unordered_map> #include <utility> #include <algorithm> #include <climits> using namespace std; void input(int & n, int & k, int & m, vector<array<int,3>> & jellies) { cin >> n >> k >> m; jellies.resize(n); for(int i = 0; i < n; i++) { cin >> jellies[i][0] >> jellies[i][1] >> jellies[i][2]; jellies[i][0]--; jellies[i][1] %= m; } } void split_jellies(int const k, vector<array<int,3>> const & jellies, vector<vector<pair<int,int>>> & color_jellies, bool & is_empty) { vector<vector<pair<int,int>>> temp_color_jellies(k, vector<pair<int,int>>()); for(auto const & j : jellies) { temp_color_jellies[j[0]].push_back({j[1],j[2]}); } for(int i = 0; i < k; i++) { if(temp_color_jellies[i].empty()) { is_empty = 1; break; } sort(temp_color_jellies[i].begin(), temp_color_jellies[i].end()); color_jellies[i].push_back(temp_color_jellies[i][0]); for(int j = 1; j < temp_color_jellies[i].size(); j++) { if(temp_color_jellies[i][j].first != temp_color_jellies[i][j-1].first) { color_jellies[i].push_back(temp_color_jellies[i][j]); } } } } void make_group_jellies(int const k, int const m, vector<vector<pair<int,int>>> const & color_jellies, vector<pair<int,long long>> & group_jellies) { vector<vector<long long>> prices(2, vector<long long>(m, LONG_LONG_MAX / 2)); prices[1][0] = 0; for(int i = 0; i < k; i++) { fill(prices[i%2].begin(), prices[i%2].end(), LONG_LONG_MAX / 2); for(auto const & jel : color_jellies[i]) { for(int j = 0; j < m; j++) { if(prices[(i+1)%2][j] == LONG_LONG_MAX / 2) { continue; } int cur_weight = (jel.first + j) % m; long long cur_price = prices[(i+1)%2][j] + jel.second; if(prices[i%2][cur_weight] > cur_price) { prices[i%2][cur_weight] = cur_price; } } } } for(int i = 1; i < m; i++) { if(prices[(k-1)%2][i] < LONG_LONG_MAX / 2) { group_jellies.push_back({i,prices[(k-1)%2][i]}); } } group_jellies.push_back({0, 0}); } void make_cheapest_single(int const m, vector<pair<int,long long>> const & group_jellies, vector<long long> & cheapest_single) { cheapest_single.resize(m, LONG_LONG_MAX / 2); for(auto const & grp : group_jellies) { int cur_weight = grp.first; long long cur_price = grp.second; do { if(cheapest_single[cur_weight] > cur_price) { cheapest_single[cur_weight] = cur_price; } cur_weight = (cur_weight + grp.first) % m; cur_price += grp.second; } while(cur_weight); } } void make_result(int const m, vector<long long> const & cheapest_single, vector<long long> & result) { vector<vector<long long>> temp_result(2, vector<long long>(m, LONG_LONG_MAX / 2)); temp_result[0][0] = 0; temp_result[1][0] = 0; for(int i = 1; i < m; i++) { for(int j = 1; j < m; j++) { temp_result[i%2][j] = min(temp_result[(i+1)%2][j], temp_result[(i+1)%2][(j+m-i)%m]+cheapest_single[i]); } } result = move(temp_result[(m-1)%2]); } void solve() { int n, k, m; vector<array<int,3>> jellies; input(n, k, m, jellies); vector<vector<pair<int,int>>> color_jellies(k, vector<pair<int,int>>()); bool is_empty = 0; split_jellies(k, jellies, color_jellies, is_empty); if(is_empty) { cout << 0 << "\n"; for(int i = 1; i < m; i++) { cout << -1 << "\n"; } return; } vector<pair<int,long long>> group_jellies; make_group_jellies(k, m, color_jellies, group_jellies); vector<long long> cheapest_single; make_cheapest_single(m, group_jellies, cheapest_single); vector<long long> result; make_result(m, cheapest_single, result); for(int i = 0; i < m; i++) { cout << ((result[i] == (LONG_LONG_MAX / 2)) ? -1 : result[i]) << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); solve(); 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 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 | #include <iostream> #include <vector> #include <array> #include <unordered_map> #include <utility> #include <algorithm> #include <climits> using namespace std; void input(int & n, int & k, int & m, vector<array<int,3>> & jellies) { cin >> n >> k >> m; jellies.resize(n); for(int i = 0; i < n; i++) { cin >> jellies[i][0] >> jellies[i][1] >> jellies[i][2]; jellies[i][0]--; jellies[i][1] %= m; } } void split_jellies(int const k, vector<array<int,3>> const & jellies, vector<vector<pair<int,int>>> & color_jellies, bool & is_empty) { vector<vector<pair<int,int>>> temp_color_jellies(k, vector<pair<int,int>>()); for(auto const & j : jellies) { temp_color_jellies[j[0]].push_back({j[1],j[2]}); } for(int i = 0; i < k; i++) { if(temp_color_jellies[i].empty()) { is_empty = 1; break; } sort(temp_color_jellies[i].begin(), temp_color_jellies[i].end()); color_jellies[i].push_back(temp_color_jellies[i][0]); for(int j = 1; j < temp_color_jellies[i].size(); j++) { if(temp_color_jellies[i][j].first != temp_color_jellies[i][j-1].first) { color_jellies[i].push_back(temp_color_jellies[i][j]); } } } } void make_group_jellies(int const k, int const m, vector<vector<pair<int,int>>> const & color_jellies, vector<pair<int,long long>> & group_jellies) { vector<vector<long long>> prices(2, vector<long long>(m, LONG_LONG_MAX / 2)); prices[1][0] = 0; for(int i = 0; i < k; i++) { fill(prices[i%2].begin(), prices[i%2].end(), LONG_LONG_MAX / 2); for(auto const & jel : color_jellies[i]) { for(int j = 0; j < m; j++) { if(prices[(i+1)%2][j] == LONG_LONG_MAX / 2) { continue; } int cur_weight = (jel.first + j) % m; long long cur_price = prices[(i+1)%2][j] + jel.second; if(prices[i%2][cur_weight] > cur_price) { prices[i%2][cur_weight] = cur_price; } } } } for(int i = 1; i < m; i++) { if(prices[(k-1)%2][i] < LONG_LONG_MAX / 2) { group_jellies.push_back({i,prices[(k-1)%2][i]}); } } group_jellies.push_back({0, 0}); } void make_cheapest_single(int const m, vector<pair<int,long long>> const & group_jellies, vector<long long> & cheapest_single) { cheapest_single.resize(m, LONG_LONG_MAX / 2); for(auto const & grp : group_jellies) { int cur_weight = grp.first; long long cur_price = grp.second; do { if(cheapest_single[cur_weight] > cur_price) { cheapest_single[cur_weight] = cur_price; } cur_weight = (cur_weight + grp.first) % m; cur_price += grp.second; } while(cur_weight); } } void make_result(int const m, vector<long long> const & cheapest_single, vector<long long> & result) { vector<vector<long long>> temp_result(2, vector<long long>(m, LONG_LONG_MAX / 2)); temp_result[0][0] = 0; temp_result[1][0] = 0; for(int i = 1; i < m; i++) { for(int j = 1; j < m; j++) { temp_result[i%2][j] = min(temp_result[(i+1)%2][j], temp_result[(i+1)%2][(j+m-i)%m]+cheapest_single[i]); } } result = move(temp_result[(m-1)%2]); } void solve() { int n, k, m; vector<array<int,3>> jellies; input(n, k, m, jellies); vector<vector<pair<int,int>>> color_jellies(k, vector<pair<int,int>>()); bool is_empty = 0; split_jellies(k, jellies, color_jellies, is_empty); if(is_empty) { cout << 0 << "\n"; for(int i = 1; i < m; i++) { cout << -1 << "\n"; } return; } vector<pair<int,long long>> group_jellies; make_group_jellies(k, m, color_jellies, group_jellies); vector<long long> cheapest_single; make_cheapest_single(m, group_jellies, cheapest_single); vector<long long> result; make_result(m, cheapest_single, result); for(int i = 0; i < m; i++) { cout << ((result[i] == (LONG_LONG_MAX / 2)) ? -1 : result[i]) << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |