Test instructions
Gives the n set of data with one type for each set of data.
0 represents at least one, 1 represents at most one, and 2 represents any selection.
Give the backpack capacity.
If the backpack does not meet the most basic requirements output-1.
Ideas:
Backpack Problem disguised Inspection ~
When 0 is initialized to-inf, then it is guaranteed to select at least one.
When 1 or 2 initializes the value of the previous layer, then 1 and 2 are slightly different, 1 can only get the next layer from the previous layer, 2 may be updated with this layer.
#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;intc[ the],w[ the];intdp[ the][ the];intMain () {intn,t; while(SCANF ("%d%d", &n,&t)! =EOF) {memset (DP,0,sizeof(DP)); for(intI=1; i<=n;i++) { intm,s; scanf ("%d%d",&m,&s); for(intj=1; j<=m;j++) {scanf ("%d%d",&w[j],&C[j]); } if(s==0) { for(intj=0; j<=t;j++) {Dp[i][j]=-999999999; } for(intj=1; j<=m;j++) { for(intk=t;k>=w[j];k--) { //Dp[i][k]=max (Dp[i][k],dp[i-1][k-w[j]]+c[j]); //Dp[i][k]=max (Dp[i][k],dp[i][k-w[j]]+c[j]);Dp[i][k]=max (Dp[i][k],max (dp[i-1][k-w[j]]+c[j],dp[i][k-w[j]]+c[j])); } } } Else if(s==1) { for(intj=0; j<=t;j++) {Dp[i][j]=dp[i-1][j]; } for(intj=1; j<=m;j++) { for(intk=t;k>=w[j];k--) {Dp[i][k]=max (dp[i][k],dp[i-1][k-w[j]]+C[j]); } } } Else { for(intj=0; j<=t;j++) {Dp[i][j]=dp[i-1][j]; } for(intj=1; j<=m;j++) { for(intk=t;k>=w[j];k--) {Dp[i][k]=max (dp[i][k],dp[i][k-w[j]]+C[j]); } } } } if(dp[n][t]<0) printf ("-1\n"); Elseprintf ("%d\n", dp[n][t]); }}
HDU 3535 "Backpack"