标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2853 Accepted Submission(s): 1018
1 //很明显是01背包问题,但本题数据太大普通的01背包会超时,由于v和c都不大于10,最多有11*11组数据,统计每组数据有多少个,所以本题可以看做是多重背包 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 using namespace std; 6 int f[10004]; 7 int N,C; 8 void zeroonepack(int v,int m,int ttl) 9 { 10 for(int i=ttl;i>=v;i--) 11 f[i]=max(f[i],f[i-v]+m); 12 } 13 void complitpack(int v,int m,int ttl) 14 { 15 for(int i=v;i<=ttl;i++) 16 f[i]=max(f[i],f[i-v]+m); 17 } 18 void multipack(int v,int m,int c,int ttl) 19 { 20 if(c*v>=ttl) 21 complitpack(v,m,ttl); 22 else 23 { 24 int k=1; 25 while(k<c) 26 { 27 zeroonepack(k*v,k*m,ttl); 28 c-=k; 29 k*=2; 30 } 31 zeroonepack(c*v,c*m,ttl); 32 } 33 } 34 int main() 35 { 36 char s[15]; 37 int val,cont; 38 int eg[12][12]; 39 while(scanf("%d%d",&N,&C)!=EOF){ 40 memset(f,0,sizeof(f)); 41 memset(eg,0,sizeof(eg)); 42 for(int i=1;i<=N;i++) 43 { 44 scanf("%s%d%d",s,&val,&cont); 45 eg[val][cont]++; 46 } 47 for(int i=0;i<=10;i++) 48 { 49 for(int j=0;j<=10;j++) 50 { 51 if(eg[i][j]==0) continue; 52 multipack(j,i,eg[i][j],C); 53 } 54 } 55 printf("%d\n",f[C]); 56 } 57 return 0; 58 }
标签:
原文地址:http://www.cnblogs.com/--ZHIYUAN/p/5982040.html