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