Hdu 2602 Bone Collector simple dp question 0-1 backpack

Source: Internet
Author: User
Bone Collector

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 14600 Accepted Submission (s): 5817


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
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

Although it is simple, it has been done for a long time before it is made out. In the middle, wa is made several times. 1. The standard template does not take into account the vol = 0, so many items are valuable, but the wood has a volume.

2. A few tragedies have been cleared when the array is cleared.

# Include <stdio. h> # include <stdio. h> struct node {int weight, value;} a [1100]; int ans [1100]; int main () {int x, n, vol, I, j; scanf ("% d", & x); while (x --) {scanf ("% d", & n, & vol); for (I = 1; I <= n; I ++) scanf ("% d", & a [I]. value); for (I = 1; I <= n; I ++) scanf ("% d", & a [I]. weight); if (vol = 0) {// In special cases where the volume is 0, note that there are items with value but the volume is 0 int sum = 0; for (I = 1; I <= n; I ++) {if (a [I]. weight = 0) sum + = a [I]. value;} printf ("% d \ n", sum); continu E;} for (I = 1; I <= vol; I ++) ans [I] =-1; ans [0] = 0; for (j = 1; j <= n; j ++) // the template of the 0-1 backpack is moved to {for (I = vol; I> = 0; I --) {if (ans [I]! =-1) {if (I + a [j]. weight <= vol & (ans [I] + a [j]. value> ans [I + a [j]. weight]) ans [I + a [j]. weight] = (ans [I] + a [j]. value) ;}} int max = 0; for (I = 1; I <= vol; I ++) if (ans [I]> max) max = ans [I]; printf ("% d \ n", max );}}

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.