Test instructions: Input t,t group sample, each set of sample input N, V, K. then enter the value of n items, and then enter the volume of n items. The solution of K-optimization is obtained.
Analysis: Dp[n][v][k] represents n items, in the case of a volume not exceeding V, the value of the K-large is how much. DP[I][V][K] and Dp[i-1][v][k] and dp[i-1][v-volume[i]]+value[i].
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespacestd;Const intMAXN = the;intVALUE[MAXN];intVOLUME[MAXN];intdp[maxn*Ten][ -];inta[ -];BOOLcmpintX1,intx2) { returnX1>x2;}intMain () {intN, v, K; intT; scanf ("%d", &t); while(t--) {scanf ("%d%d%d", &n, &v, &k); for(intI=1; i<=n; i++) scanf ("%d", &Value[i]); for(intI=1; i<=n; i++) scanf ("%d", &Volume[i]); Memset (DP,0,sizeof(DP)); for(intI=1; i<=n; i++) { for(intl=0; j>=volume[i]; j--) { intKK =1; for(intij=1; ij<=k; ij++) {A[kk+ +] = dp[j-volume[i]][ij]+Value[i]; A[kk++] =Dp[j][ij]; } sort (A+1, A +KK, CMP); KK= Unique (A +1, A+KK)-a-1; for(intij=1; ij<=k; ij++) { if(ij>KK) Dp[j][ij]=0; ElseDp[j][ij]=A[ij]; }}} printf ("%d\n", Dp[v][k]); } return 0;}
HDU 2639 Bone Collector II (DP K-Optimal solution)