Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=2639
Key codes for K-Optimal solutions:
Use two arrays to record two states (select or not), and just record the first k times. Both arrays are the best possible solutions for the first K-times. So all we have to do is compare the two arrays and go straight to K.
Title Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 intdp[1005][ *],a[ *],b[ *],val[ the],vol[ the];6 intMain ()7 {8 intt,n,v,k,c,d,e;9 while(~SCANF ("%d",&t))Ten { One while(t--) A { -scanf"%d%d%d",&n,&v,&k); - for(intI=0; i<n;i++) thescanf"%d",&val[i]); - for(intI=0; i<n;i++) -scanf"%d",&vol[i]); -Memset (DP,0,sizeof(DP)); +Memset (A,0,sizeof(a)); -memset (b,0,sizeof(b)); + for(intI=0; i<n;i++) A { at for(intj=v;j>=vol[i];j--) - { - for(intt=1; t<=k;t++) - { -a[t]=dp[j-vol[i]][t]+Val[i]; -b[t]=Dp[j][t]; in } -C=d=e=1; to while(e<=k&& (c!=k+1|| d!=k+1)) + { - if(a[c]>B[d]) theDp[j][e]=a[c++]; * Else $dp[j][e]=b[d++];Panax Notoginseng if(dp[j][e]!=dp[j][e-1]) -e++; the } + } A } theprintf"%d\n", Dp[v][k]); + } - } $ return 0; $}
View Code
2639-bone Collector II (01-Pack K-optimal solution)