HDU 2159 fate backpack Problems

Source: Internet
Author: User

We regard patience as the size of the backpack, experience as the value, and increase the number of one-dimensional constraints. This is a typical knapsack problem.

We define DP [J] [k] to indicate that the backpack capacity is J, and select the maximum value that K items can achieve. In fact, this is the result of the previous dimension omitted. We can clearly define DP [I] [J] [k] to indicate the items in the previous I, for a backpack with a size of J, select the maximum value that K items can reach. After we list the state transition equation, we find that we can omit the previous one to ensure the correctness of the answer, therefore, we use the first definition to optimize the space.

Status transfer is relatively simple.CodeYou will know.

# Include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> using namespace STD; const int max = 110; int DP [Max] [Max]; int main () {int n, m, K, s; int V [Max], W [Max]; while (scanf ("% d ", & N, & M, & K, & S )! = EOF) {for (INT I = 1; I <= K; I ++) {scanf ("% d", & W [I], & V [I]);} memset (DP, 0, sizeof (DP); int ans =-1; bool flag = false; For (Int J = 1; j <= m; j ++) {for (INT I = 1; I <= K; I ++) {for (int K = 1; k <= s; k ++) {for (INT x = 0; x <= K & x * V [I] <= J; X ++) {DP [J] [k] = max (DP [J] [K], DP [J-x * V [I] [k-x] + x * W [I]); If (DP [J] [k]> = N) {flag = true; ans = J; break;} If (FLAG) printf ("% d \ n", M-ans); else printf ("-1 \ n");} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.