HDU 2602 Bone Collector (Classic 01 Backpack Problem)

Topic link:

http://acm.hdu.edu.cn/showproblem.php?pid=2602

Bone Collector

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


Problem Description
Many years 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 , obviously , 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 ?
 

 

Input
The first line contain a integer T , the number of cases.
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.
 

 

Output
One integer per line representing the maximum of the total value (this number will be less than 2 31 ).
 

 

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

 

Sample Output
14
 

 

Author
Teddy
 

 

Source
 
analyze:
The classic 01 backpack problem, each item has only two states, whether to put it or not
dp[i][j]: That is, the maximum value that can be obtained by putting the previous i items into a backpack with a capacity of j
State transition equation:
dp[i][j]=dp[i-1][j]  j<w[i]
dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]     j>=w[i]
 
Notice:
1. The value of the item is entered first, not the weight. The title seems to say the opposite.
2. The capacity of the backpack can be 0, and the weight of the item can also be 0
Test data about point 2:
enter
1
5 0
2 4 1 5 1
0 0 1 0 0
output
12
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,c;
        scanf("%d %d",&n,&c);
        int v[n+1],w[n+1];
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&v[i]);
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&w[i]);
        }
        int dp[n+1][c+1];
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=c;j++)
            {
                if (w[i]<=j) // Indicates that the i-th item is put into the backpack 
                {
                    dp[i][j] =max(dp[i- 1 ][j],dp[i- 1 ][jw[i]]+v[i]); // After the i-th item is placed, then The first i-1 items may not be put in because the remaining space is not enough 
                } else // Indicates that the i-th item is not put into the backpack 
                {
                    dp[i][j] =dp[i- 1 ][j]; // If the i-th item is not put into the backpack, then the maximum value at this time is equal to the value of the first i-1 item 
                }
            }
        }
        printf("%d\n",dp[n][c]);
    }
    return 0;
}

/*
3
5 10
6 3 5 4 6
2 2 6 5 4
5 10
1 2 3 4 5
5 4 3 2 1
5 0
2 4 1 5 1
0 0 1 0 0


15
14
12
*/

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326584699&siteId=291194637