标签:
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3355 Accepted Submission(s):
1726
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int main() 6 { 7 int T,i,j,n,m,v,k,kk; 8 int dp[1005][35],val[1005],vol[1005],A[35],B[35]; 9 scanf("%d",&T); 10 while(T--) 11 { 12 scanf("%d%d%d",&n,&v,&k); 13 for(i=0; i<n; i++) 14 scanf("%d",&val[i]); //价值 15 for(i=0; i<n; i++) 16 scanf("%d",&vol[i]); //体积 17 memset(dp,0,sizeof(dp)); 18 int a,b,c; 19 for(i=0; i<n; i++) 20 for(j=v; j>=vol[i]; j--) 21 { 22 for(kk=1; kk<=k; kk++) //从最优的向后循环 23 { 24 A[kk]=dp[j-vol[i]][kk]+val[i]; //选中i物体 25 B[kk]=dp[j][kk]; //不选i物体 26 } 27 A[kk]=-1; //-1标记为结尾 28 B[kk]=-1; 29 a=b=c=1; 30 while(c<=k&&(A[a]!=-1||B[b]!=-1)) 31 { 32 if(A[a]>B[b]) //大的数排在前面 33 dp[j][c]=A[a++]; 34 else 35 dp[j][c]=B[b++]; 36 if(dp[j][c]!=dp[j][c-1]) //如果两数相同,则只记录一次 37 c++; 38 } 39 } 40 printf("%d\n",dp[v][k]); 41 } 42 return 0; 43 }
hdu 2639 Bone Collector II(01背包 第K大价值)
标签:
原文地址:http://www.cnblogs.com/pshw/p/5033088.html