2016HUAS_ACM Summer Camp 4M-Basic DP

Source: Internet
Author: User

Simple 0-1 knapsack problem, everyone will do. Test instructions don't want to explain too much.

Briefly describe the case of the topic and take several key

Sample Input

1//Number of test groups T

5 10//Total number of bones N, total backpack volume V

1 2 3 4 5//n block Bone individual values

5 4 3 2 1//n block bone Individual volume

Sample output//Outputs maximum value

14

Note: Each bone has only one piece, otherwise the output of the case can not be understood as 14.

The value and volume of the bone must be entered separately.

The most essence of it should be this sentence, but I do not know how to digest =_=| |

Dp[j]=max (dp[j],dp[j-volume[i]]+value[i]);

#include <iostream>#include<cstring>using namespacestd;#defineMAXN 1001intN,t,v,i,j,ans;intVALUE[MAXN],VOLUME[MAXN],DP[MAXN];intMaxintAintb) {    returnA>b?a:b;}voidDP (intNintv) {ans=0; Memset (DP,0,sizeof(DP));  for(i=1; i<=n;i++)         for(j=v;j>=volume[i];j--) {Dp[j]=max (dp[j],dp[j-volume[i]]+Value[i]); Ans=Max (ans,dp[j]); } cout<<ans<<Endl;}intMain () {CIN>>u;  while(t--) {cin>>N>>W;  for(i=1; i<=n;i++) Cin>>Value[i];  for(i=1; i<=n;i++) Cin>>Volume[i];    DP (N,V); }    return 0;}

2016HUAS_ACM Summer Camp 4M-Basic DP

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.