Hdu 2602 Bone Collector, hdu2602

Source: Internet
Author: User

Hdu 2602 Bone Collector, hdu2602

The simple 01 backpack is very clear.

I have a suggestion to answer all questions about a backpack.

Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 27979 Accepted Submission (s): 11404


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
/* Problem Analysis: Enter the value of the bone in the second row [1001], in the second line, input volume of the bone [1001]. We want to obtain the state equation dp [I] [j] = max (dp [I-1] [j], dp [I-1] [j-volume [I] + value [I]) equation explanation: when loading the I bone, we should consider the value of the dp [I] [j] for loading the I bone, we need to compare the value of loading I with that without installing I. If I is not loaded, the I-1 is loaded into the package with a volume of j. When I is loaded, the I-1 is loaded into the package with a volume of j-volume [I ]., however, because I is loaded, value [I] is added. */# include <iostream> # include <cstring> using namespace std; int max (int, int B) {if (a> B) return a; else return B;} int main (int I, int j) {int t; // test example int n; // total number of bones int v; // total package volume int static dp [1001] [1001]; // The final maximum value int value [1001]; // value of a single bone int volume [1001]; // volume of a single bone cin> t; while (t --) {cin> n> v; for (I = 1; I <= n; I ++) cin> value [I]; for (I = 1; I <= n; I ++) cin> volume [I]; memset (dp, 0, sizeof (dp); for (I = 1; I <= n; I ++) {for (j = 0; j <= v; j ++) {if (j> = volume [I]) dp [I] [j] = max (dp [I-1] [j], dp [I-1] [j-volume [I] + value [I]); else dp [I] [j] = dp [I-1] [j] ;}} cout <dp [n] [v] <endl ;}return 0 ;}

 



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;

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.

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.