HDU 2602 Bone Collector 0/1 backpack

Source: Internet
Author: User

Link: HDU 2602 Bone Collector

Bone Collector Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 28903 accepted submission (s): 11789


Problem descriptionpolicyears ago, in Teddy's hometown there was a man who was called "Bone Collector ". this man like to 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 his trip of collecting there are a lot of bones, obviusly, different bone has different value and different volume, now given the each bone's value along his trip, Can you calculate out the maximum of the total value the bone collector can get?


Inputthe first line contain a integer t, the number of instances.
Followed by T cases, each case three lines, the first line contain two integer N, V, (n <= 1000, v <= 1000) representing the number of bones and the volume of his bag. and the second line contain N integers representing the value of each bone. the third line contain N integers representing the volume of each bone.
Outputone integer per line representing the maximum of the total value (This number will be less than 231 ).
Sample Input
 
15 101 2 3 4 55 4 3 2 1

Sample output
 
14

Authorteddy
Sourcehdu 1st "vegetable-birds Cup" programming open contest
Recommendlcy | we have carefully selected several similar problems for you: 1203 2159 2955 2844
Classic 0/1 backpack problems.

There are n items and a backpack with a capacity of V. The size of the I-item is V [I], and the value is W [I]. solving which items are loaded into a backpack can maximize the total value.

This is the most basic backpack problem. There is only one item for each type, and there are only two statuses: Put and not put.

Use the sub-problem definition status: DP [I] [v] to indicate the maximum value that can be obtained when I items are placed in a package with a capacity of M.

State transition equation: DP [I] [m] = max (DP [I-1] [m], DP [I-1] [M-V [I] + W [I]);

It can be converted to one-dimensional scenario: DP [m] = max (DP [m], DP [M-V [I] + W [I]);

Code:

 
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; int N, V, DP [1010], value [1010], volume [1010]; int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & V); For (INT I = 1; I <= N; I ++) scanf ("% d", & value [I]); for (INT I = 1; I <= N; I ++) scanf ("% d", & volume [I]); memset (DP, 0, sizeof (DP); For (INT I = 1; I <= N; I ++) for (Int J = V; j> = Volume [I]; j --) DP [J] = max (DP [J], DP [J-volume [I] + value [I]); printf ("% d \ n ", DP [v]);} 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.