HDU2602 01 Backpack Primer Bone Collector "01 Backpack template Problem"

Source: Internet
Author: User

problem Description
Many years ago, in Teddy ' s hometown there were a man he 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 bone s, obviously, different bone have different value and different volume, now given the all bone ' s value along his trip, Can calculate out the maximum of the total value the bone collector can get?

Input
The 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 <= 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 for each bone.

Output
One integer per line representing the maximum of the total value (this number would be is less than 231).

Sample Input
1
5 10
1 2 3) 4 5
5 4 3) 2 1

Sample Output
14**

Test instructions: Backpack with a capacity of V, as much as possible to load more stone, output backpack the greatest value
Train of thought: the classic routine of the 01 backpack, pushed by the stone 1 to the stone N, under the I-stone backpack capacity v maximum value by i-1 recursion

 #include <stdio.h> #include <string.h> #define N + int num[n+10],w[n+10],v[n+
10];
    int main () {int i, J;
    int T, n, V;
    scanf ("%d", &t);
        while (T--) {memset (num,0,sizeof (num));
        Memset (W,0,sizeof (w));
        memset (v,0,sizeof (v));
        scanf ("%d%d", &n,&v);
        for (i = 1; I <= n; i + +) scanf ("%d", &w[i]);
        for (i = 1; I <= n; i + +) scanf ("%d", &v[i]); for (i = 1; I <= n; i + +) for (j = V; J >=v[i];j-)/*j represents the remaining capacity of the backpack */{NUM[J]
                = Max (num[j],num[j-v[i]]+w[i]);
            /*NUM[J] means that the value of the first stone is not put into the backpack *//*num[j-v[i]]+w[i] means the value of the first stone into the backpack */* Compare the best solution of the two to deposit num[j]*/
    } printf ("%d\n", Num[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.