首页 > 代码库 > HDU3732 背包DP
HDU3732 背包DP
Ahui Writes Word
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2853 Accepted Submission(s): 1018
Problem Description
We
all know that English is very important, so Ahui strive for this in
order to learn more English words. To know that word has its value and
complexity of writing (the length of each word does not exceed 10 by
only lowercase letters), Ahui wrote the complexity of the total is less
than or equal to C.
Question: the maximum value Ahui can get.
Note: input words will not be the same.
Question: the maximum value Ahui can get.
Note: input words will not be the same.
Input
The
first line of each test case are two integer N , C, representing the
number of Ahui’s words and the total complexity of written words. (1 ≤ N
≤ 100000, 1 ≤ C ≤ 10000)
Each of the next N line are a string and two integer, representing the word, the value(Vi ) and the complexity(Ci ). (0 ≤ Vi , Ci ≤ 10)
Each of the next N line are a string and two integer, representing the word, the value(Vi ) and the complexity(Ci ). (0 ≤ Vi , Ci ≤ 10)
Output
Output the maximum value in a single line for each test case.
Sample Input
5 20
go
5 8
think 3 7
big 7 4
read 2 6
write 3 5
Sample Output
15
Hint
Input data is huge,please use “scanf(“%s”,s)”
Author
Ahui
题意:
每背一个单词有价值和花费,给出单词数和最大花费,问得到的最大价值。
代码:
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 }
HDU3732 背包DP
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。