Hangzhou Electric 2603 Bone Collector (Simple 01 backpack)

Source: Internet
Author: User

Bone CollectorTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 35296 Accepted Submission (s): 14531


problem DescriptionMany years ago, in Teddy ' s hometown there is a man who was called "Bone Collector". 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 of collecting there is a lot of bones, obviously , different bone have different value and different volume, now given the each bone's value along his trips, can you CALCU Late out the maximum of the total value the bone collector can get?

 
InputThe first line contain a integer T, the number of cases.
Followed by T cases, each case three lines, the first line contain both integer n, V, (N <=, v <=) repr Esenting 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 for each bone. 
OutputOne integer per line representing the maximum of the total value (this number would be is less than 231). 
Sample Input
15 101 2 3 4 55 4 3 2 1
 
Sample Output
14
 
A very bare 01 backpack, note the inner for Loop (for (int j = v; j>=weight[i];j--))

AC Code:
#include <cstdio> #include <iostream> #include <cstring>using namespace std; int dp[10000]; int value[ 1005],WEIGHT[1005]; int T,n,v;int main () {    while (scanf ("%d", &t)!=eof)    {while        (t--)        {            scanf ("%d%d", &n,&v);            for (int i = 0; i<n; i++)                scanf ("%d", &value[i]);            for (int j = 0; j<n; j + +)                scanf ("%d", &weight[j]);            Memset (Dp,0,sizeof (DP));            for (int i = 0; i<n; i++)            {for                (int j = v; j>=weight[i]; j--)                {                    Dp[j] = max (Dp[j],dp[j-weight[i]) +value[i]);                }            }            printf ("%d\n", Dp[v]);        }    }    return 0;}


Hangzhou Electric 2603 Bone Collector (Simple 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.