Hdoj 2620 Bone Collector (0-1 backpack)

Source: Internet
Author: User

title Link:http://acm.hdu.edu.cn/showproblem.php?pid=2602

Thinking Analysis: The problem is a classic 0-1 knapsack problem; Assuming state Dp[i][v] indicates the maximum value that the first I item fits into a backpack with a capacity of V, then the DP recurrence formula can be deduced

Dp[i][v] = Max{dp[i-1][v], Dp[i-1][v–c[i]] + w[i]};c[i) indicates the capacity of the article I, w[i] denotes the value of article I items; the decision for each stage of the dynamic planning issue is whether to

Select the item I to put in the backpack, if I do not select the item I, then dp[i][v] = Dp[i-1][v], if you select the item I put into the backpack, then Dp[i][v] = Dp[i-1][v-c[i]], v-c[i] >= 0;

At the same time, you can use a one-dimensional array recursion, because recursion I, only need to use to the i-1 line of array elements, so can be pushed from V down to 0, recursive formula is as follows: dp[v] = MAX (Dp[v], dp[v-c[i] + w[i]),

(one-dimensional DP) code is as follows:

ImportJava.util.*; Public classMain {Static Final intMax_n = 1000 + 10; Static int[] Value =New int[Max_n]; Static int[] Volumn =New int[Max_n]; Static int[] DP =New int[Max_n];  Public Static intMax (intAintb) {returna > B?a:b; }     Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intCase_times =In.nextint ();  while(case_times--! = 0) {            intN, V;  for(inti = 0; i < max_n; ++i) dp[i]= 0; N=In.nextint (); V=In.nextint ();  for(inti = 1; I <= N; ++i) value[i]=In.nextint ();  for(inti = 1; I <= N; ++i) volumn[i]=In.nextint ();  for(inti = 1; I <= N; ++i) { for(intv = v; V >= 0; --v) {if(V-volumn[i] >= 0) Dp[v]= Max (Dp[v], Dp[v-volumn[i]] +Value[i]);        }} System.out.println (Dp[v]); }    }}

The (two-dimensional DP) code is as follows:

ImportJava.util.*; Public classMain {Static Final intMax_n = 1000 + 10; Static int[] Value =New int[Max_n]; Static int[] Volumn =New int[Max_n]; Static int[] DP =New int[Max_n][max_n];  Public Static intMax (intAintb) {returna > B?a:b; }     Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intCase_times =In.nextint ();  while(case_times--! = 0) {            intN, V;  for(inti = 0; i < max_n; ++i) { for(intj = 0; J < Max_n; ++j) Dp[i][j]= 0; } N=In.nextint (); V=In.nextint ();  for(inti = 1; I <= N; ++i) value[i]=In.nextint ();  for(inti = 1; I <= N; ++i) volumn[i]=In.nextint ();  for(inti = 1; I <= N; ++i) { for(intv = 0; V <= v; ++v) {if(V-volumn[i] >= 0) Dp[i][v]= Max (Dp[i-1][v], Dp[i-1][v-volumn[i]] +Value[i]); ElseDp[i][v]= Dp[i-1][v];        }} System.out.println (Dp[n][v]); }    }}

Hdoj 2620 Bone Collector (0-1 backpack)

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.