DP regain-01 backpack--hdu 2602

Source: Internet
Author: User

Many 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 less than 2 to). Sample INPUT15 101 2 3 4 4 3 2 1 sample Output14 long time did not brush DP problem, first a 01 backpack preheating
#include <iostream>#include<cstring>#include<stdlib.h>#include<stdio.h>using namespacestd;intdp[1001][1001];intw[1001],v[1001],w,n;intMaxintAintb) {    if(a>=b)returnA; Else returnb;}voidsolve () { for(inti=n-1; i>=0; i--)    {         for(intj=0; j<=w;j++)        {            if(J<w[i]) dp[i][j]=dp[i+1][j]; ElseDp[i][j]=max (dp[i+1][j],dp[i+1][j-w[i]]+V[i]); }} cout<<dp[0][w]<<Endl;}intMain () {inti,j,k; scanf ("%d",&k);  for(i=0; i<k;i++) {scanf ("%d%d",&n,&v);  for(j=0; j<n;j++) {scanf ("%d",&V[j]); }         for(j=0; j<n;j++) {scanf ("%d",&W[j]);        } solve (); W=n=0; Memset (DP,0,sizeof(DP)); memset (V,0,sizeof(v)); Memset (W,0,sizeof(w)); }    return 0;}
View Code

DP regain-01 backpack--hdu 2602

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.