HDU 2602 Bone Collector 0/1 backpack, hdu2602
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%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;}
Hangzhou electric 2602 Bone Collector 01 backpack problem why has it been wrong?
The following error occurs:
1. The subscript of the opening array unit cannot be a variable, so the statement
Const int maxn = 1005;
Should be changed:
# Define maxn1005
2. I do not know what the development environment is like. If it is 16 bits,
So
Dp [1005] [1005]
The compilation mode needs to be changed because the value is too large. Otherwise, an error occurs.
Hdu 2602 Why WA?
I will send you the ac code for reference. Let's take a look at the comparison. Come on.
# Include <stdio. h>
# Include <string. h>
Int p [1001];
Struct pp
{
Int n;
Int v;
} A [1001];
Int MAX (int m, int n)
{
Return m> n? M: n;
}
Int main ()
{
Int t, m, v, I, j;
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d", & m, & v );
For (I = 1; I <= m; I ++)
{
Scanf ("% d", & a [I]. n );
}
For (I = 1; I <= m; I ++)
{
Scanf ("% d", & a [I]. v );
}
Memset (p, 0, sizeof (p ));
For (I = 1; I <= m; I ++)
{
For (j = v; j> = 0; j --)
If (j> = a [I]. v)
P [j] = MAX (p [j], p [j-a [I]. v] + a [I]. n );
}
Printf ("% d \ n", p [v]);
}
Return 0;