标签:
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3471 Accepted Submission(s): 1792
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 int dp[1002][32]; 7 int val[102],vol[102]; 8 int n,v,K; 9 int a[32],b[32]; 10 int main() 11 { 12 freopen("in.txt","r",stdin); 13 int i,j,k; 14 int x,y,z; 15 int T; 16 scanf("%d",&T); 17 while(T--) 18 { 19 scanf("%d%d%d",&n,&v,&K); 20 for(i=1;i<=n;i++) 21 scanf("%d",&val[i]); 22 for(i=1;i<=n;i++) 23 scanf("%d",&vol[i]); 24 memset(dp,0,sizeof(dp)); 25 for(i=1;i<=n;i++) 26 { 27 for(j=v;j>=vol[i];j--) 28 { 29 for(k=1;k<=K;k++) //记录每种状态的k优解 30 { 31 a[k]=dp[j][k]; 32 b[k]=dp[j-vol[i]][k]+val[i]; 33 } 34 a[k]=b[k]=-1; //存储的内容已经按照从小到大排序好了,然后合并到dp数组中去 35 x=y=z=1; 36 while(z<=K&&(a[x]!=-1||b[y]!=-1)) 37 { 38 if(a[x]>b[y]) 39 { 40 dp[j][z]=a[x]; 41 x++; 42 } 43 else 44 { 45 dp[j][z]=b[y]; 46 y++; 47 } 48 if(dp[j][z-1]!=dp[j][z]) 49 z++; 50 } 51 } 52 } 53 printf("%d\n",dp[v][K]); 54 } 55 return 0; 56 }
Bone Collector II(HDU 2639 DP)
标签:
原文地址:http://www.cnblogs.com/a1225234/p/5243443.html