Hdoj 2602 Bone collector--01 Backpack

Source: Internet
Author: User

Source of the topic:HPU 2602-- Bone Collector

Problem Description

Manyyears ago, in Teddy's hometown there was a man who was called "Bonecollector". Collect varies of bones, such as dog ' s, cow ' s, also he went to the grave...
The bone collector had a big bag with a volume of V, and along he trip ofcollecting there is a lot of bones, obviously , different bone have differentvalue and different volume, now given the each bone's value along his trips, can you Calculat E out the maximum of the total value the bone collector can get?

Input

Thefirst line contain a integer T, the number of cases.
Followed by T cases, each case three lines, the first line contain Twointeger N, V, (N <=, v <=) repre Senting the number of Bonesand the volume of his bag. And the second line contain N integers representingthe value of each bone. The third line contain N integers representing thevolume of each bone.

Output

Oneinteger per line representing the maximum of the total value (this number willbe less than 231).

Sample Input

1

5 10

1 2 3) 4 5

5 4 3) 2 1

Sample Output

14

 

Research point: Dynamic Planning-01 Backpack

Main topic:

Given the T-Group test data, each set of data is given an n (a bone) and a V (the capacity of the Backpack V), and the next two lines are given the value and volume of the N-block bone respectively. The maximum value that can be loaded by the output backpack.

Topic Analysis:

dynamic Programming equation: F (n,m) =max{f (N-1,m), F (n-1,m-w[n]) +v (n,m)}, according to this recursion, we know that the 01 backpack records the optimal solution of the current position.

AC Code:

#include <cstdio> #include <cstring> #include <cstring> #include <algorithm>using namespace std int value[1005],volume[1005];//bone value, Volume int dp[1005][1005];//record Backpack volume value from 0 to V max value int main () {int t;int n,v;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&v), memset (Dp,0,sizeof (DP)),//DP array initialized to 0 for (int i=1;i<=n;i++) { scanf ("%d", &value[i]);} for (int i=1;i<=n;i++) {scanf ("%d", &volume[i]);} for (int i=1;i<=n;i++) {for (int j=0;j<=v;j++) {if (J<volume[i])//Current Backpack volume J Cannot load bones of volume Volume[i] {dp[i][j]=dp[i-1 ][J]; }else//Current Backpack Volume J can pack a bone of volume Volume[i] {//select Add or not add value for current backpack Dp[i][j]=max (Dp[i-1][j],dp[i-1][j-volume[i]]+value[i]);}}} printf ("%d\n", Dp[n][v]);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj 2602 Bone collector--01 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.