#include <iostream> #include <vector> #include <iomanip> using namespace std; void print_dp(vector<vector<long long int>>& dp, int max_colors, int max_mass) { for (int c = 0; c <= max_colors; c++) { for (int m = 0; m <= max_mass; m++) { //if (dp[c][m] != -1) { // cout << " "; //} cout << setfill('0') << std::setw(2) << dp[c][m] << " "; //cout << dp[c][m] << " "; } cout << endl; } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k, mod; cin >> n >> k >> mod; vector<vector<pair<int, int>>> items(k + 1); vector<vector<int>> prices(k + 1); vector<int> color(n); int ic, im, ip; for (int i = 0; i < n; i++) { cin >> ic >> im >> ip; items[ic].push_back(make_pair(im, ip)); } int max_mass = 2 * mod + 1; int max_bundles = mod; int max_colors = k; //vector<vector<vector<int>>> dp(max_bundles, vector<vector<int> >(max_colors, vector <int>(max_mass, -1))); vector<vector<long long int>> dp(max_colors + 1, vector <long long int>(max_mass + 1, -1)); vector<vector<int>> old_dp(max_colors, vector <int>(max_mass, -1)); vector<long long> lowest_price(mod, -1); //for (int k = 0; k < max_mass; k++) { // dp[0][k] = 0; //} dp[0][0] = 0; //for (int k = 0; k < max_colors; k++) { // dp[k][0] = -1; //} lowest_price[0] = 0; //cout << endl; //print_dp(dp, max_colors, max_mass); vector<int> dp_last_row(); for (int b = 0; b < max_bundles; b++) { //old_dp.resize() //copy(dp.begin(), dp.end(), back_inserter(old_dp)); //cout << "NEW BUNDLE" << endl; for (int c = 0; c <= max_colors; c++) { for (int m = 0; m <= max_mass; m++) { for (int i = 0; i < items[c].size(); i++) { if (m - items[c][i].first < 0 || c < 1) { continue; } int earlier_mass = (m - items[c][i].first) % mod; int potential_price = dp[c - 1][earlier_mass] + items[c][i].second; if (dp[c - 1][earlier_mass] == -1) // unreachable continue; if (potential_price < dp[c][m % mod] || dp[c][m % mod] == -1) { dp[c][m % mod] = potential_price; //print_dp(dp, max_colors, max_mass); } } } } for (int x = 0; x <= max_mass; x++) { dp[0][x] = dp[max_colors][x]; if (dp[max_colors][x] == -1) continue; if (lowest_price[x % mod] == -1) { lowest_price[x % mod] = dp[max_colors][x]; } else { lowest_price[x % mod] = min(lowest_price[x % mod], dp[max_colors][x]); } } //cout << "lowest prices" << endl; //for (int x = 0; x < mod; x++) { // cout << lowest_price[x] << " "; //} //cout << endl; //cout << endl; for (int y = 1; y <= max_colors; y++) { for (int x = 0; x <= max_mass; x++) { dp[y][x] = -1; } } } for (int x = 0; x < mod; x++) { cout << lowest_price[x] << 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | #include <iostream> #include <vector> #include <iomanip> using namespace std; void print_dp(vector<vector<long long int>>& dp, int max_colors, int max_mass) { for (int c = 0; c <= max_colors; c++) { for (int m = 0; m <= max_mass; m++) { //if (dp[c][m] != -1) { // cout << " "; //} cout << setfill('0') << std::setw(2) << dp[c][m] << " "; //cout << dp[c][m] << " "; } cout << endl; } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k, mod; cin >> n >> k >> mod; vector<vector<pair<int, int>>> items(k + 1); vector<vector<int>> prices(k + 1); vector<int> color(n); int ic, im, ip; for (int i = 0; i < n; i++) { cin >> ic >> im >> ip; items[ic].push_back(make_pair(im, ip)); } int max_mass = 2 * mod + 1; int max_bundles = mod; int max_colors = k; //vector<vector<vector<int>>> dp(max_bundles, vector<vector<int> >(max_colors, vector <int>(max_mass, -1))); vector<vector<long long int>> dp(max_colors + 1, vector <long long int>(max_mass + 1, -1)); vector<vector<int>> old_dp(max_colors, vector <int>(max_mass, -1)); vector<long long> lowest_price(mod, -1); //for (int k = 0; k < max_mass; k++) { // dp[0][k] = 0; //} dp[0][0] = 0; //for (int k = 0; k < max_colors; k++) { // dp[k][0] = -1; //} lowest_price[0] = 0; //cout << endl; //print_dp(dp, max_colors, max_mass); vector<int> dp_last_row(); for (int b = 0; b < max_bundles; b++) { //old_dp.resize() //copy(dp.begin(), dp.end(), back_inserter(old_dp)); //cout << "NEW BUNDLE" << endl; for (int c = 0; c <= max_colors; c++) { for (int m = 0; m <= max_mass; m++) { for (int i = 0; i < items[c].size(); i++) { if (m - items[c][i].first < 0 || c < 1) { continue; } int earlier_mass = (m - items[c][i].first) % mod; int potential_price = dp[c - 1][earlier_mass] + items[c][i].second; if (dp[c - 1][earlier_mass] == -1) // unreachable continue; if (potential_price < dp[c][m % mod] || dp[c][m % mod] == -1) { dp[c][m % mod] = potential_price; //print_dp(dp, max_colors, max_mass); } } } } for (int x = 0; x <= max_mass; x++) { dp[0][x] = dp[max_colors][x]; if (dp[max_colors][x] == -1) continue; if (lowest_price[x % mod] == -1) { lowest_price[x % mod] = dp[max_colors][x]; } else { lowest_price[x % mod] = min(lowest_price[x % mod], dp[max_colors][x]); } } //cout << "lowest prices" << endl; //for (int x = 0; x < mod; x++) { // cout << lowest_price[x] << " "; //} //cout << endl; //cout << endl; for (int y = 1; y <= max_colors; y++) { for (int x = 0; x <= max_mass; x++) { dp[y][x] = -1; } } } for (int x = 0; x < mod; x++) { cout << lowest_price[x] << endl; } } |