HDU_3732_(多重背包)
Ahui Writes Word
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2915 Accepted Submission(s): 1036
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
题意:每个单词有val (价值)、cos (复杂度),问消耗c的复杂度能够获得的最大价值。
最开始没注意数据规模,01背包超时,O(v*n)。
多重背包ac。
代码:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int dp[10005]; int cos,val,num[15][15]; int n,c; void zero_one(int cost,int val) { for(int j=c;j>=cost;j--) dp[j]=max(dp[j],dp[j-cost]+val); } int main() { while(scanf("%d%d",&n,&c)!=EOF) { memset(dp,0,sizeof(dp)); memset(num,0,sizeof(num)); for(int i=0;i<n;i++) { char str[20]; scanf("%s%d%d",str,&val,&cos); num[val][cos]++; } for(int i=0;i<=10;i++) for(int j=0;j<=10;j++) { if(num[i][j]>0) { if(num[i][j]*j>c) for(int k=j;k<=c;k++) dp[k]=max(dp[k],dp[k-j]+i); else { int tmp=1,nu=num[i][j]; while(nu>tmp) { zero_one(j*tmp,i*tmp); nu-=tmp; tmp=tmp<<1; } zero_one(j*nu,i*nu); } } } printf("%d\n",dp[c]); } return 0; }