标签:others pack 背包 bottom value sample nts store iostream
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2006 Accepted Submission(s): 712
#include<iostream> #include<cstdio> #include<stdlib.h> #include<cstring> using namespace std; #define INF 10000000 int numb[105],val[105]; int dp1[20005],dp2[125]; int charge[125]; void zero_one(int *dp,int can,int value,int num) { for(int j=can; j>=value*num; j--) dp[j]=min(dp[j],dp[j-value*num]+num); } void coml_package(int *dp,int can,int value) { for(int j=value; j<=can; j++) dp[j]=min(dp[j],dp[j-value]+1); } void multi_package(int *dp,int can,int value,int num) { int tmp=1; while(num>=tmp) { zero_one(dp,can,value,tmp); num-=tmp; tmp=tmp<<1; } zero_one(dp,can,value,num); } int main() { int n,k,Case=0; while(scanf("%d%d",&n,&k)!=EOF&&(n&&k)) { for(int i=0; i<20005; i++) { dp1[i]=INF; if(i<125) dp2[i]=INF; } dp1[0]=0; dp2[0]=0; int maxval=0; for(int i=0; i<n; i++) { scanf("%d",&val[i]); if(val[i]>maxval) maxval=val[i]; } for(int i=0; i<n; i++) { scanf("%d",&numb[i]); if(val[i]*numb[i]>10000) numb[i]=10000/val[i]; } for(int i=0; i<n; i++) multi_package(dp1,k+maxval,val[i],numb[i]); for(int i=0; i<n; i++) coml_package(dp2,maxval,val[i]); int res=dp1[k],g=k,r=0; for(int i=k+1; i<=k+maxval; i++) res=min(res,dp1[i]+dp2[i-k]);
if(res>=INF) printf("Case %d: -1\n",++Case); else printf("Case %d: %d\n",++Case,res); } return 0; }
标签:others pack 背包 bottom value sample nts store iostream
原文地址:http://www.cnblogs.com/jasonlixuetao/p/6386575.html