HDU 3732 Ahui Writes Word

Ahui Writes Word

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3732
64-bit integer IO format: %I64d      Java class name: Main
 
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.
 

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)
 

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)”

Source

 
解题:思想很巧妙。巧妙地将01背包转化成多重背包,由于N,C巨大,普通的01背包肯定超时。但是考虑到vi,ci不大于10,故10*10最多100种类型的单词,
 
存在很多重复的,故可以转化成多重背包,然后加上二进制优化。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 int mp[12][12],dp[10010],a[20],n,m;
18 int main() {
19     char s[30];
20     int u,v,tot;
21     while(~scanf("%d %d",&n,&m)){
22         memset(mp,0,sizeof(mp));
23         for(int i = 0; i < n; i++){
24             scanf("%s %d %d",s,&u,&v);
25             mp[u][v]++;
26         }
27         memset(dp,0,sizeof(dp));
28         for(u = 0; u < 11; u++){
29             for(v = 0; v < 11; v++){
30                 if(mp[u][v]){
31                     int t = log2(mp[u][v]);
32                     for(int k = 0; k <= t; k++){
33                         if(k == t) a[k] = mp[u][v] - (1<<k) + 1;
34                         else a[k] = 1<<k;
35                     }
36                     for(int k = 0; k <= t; k++){
37                         for(int z = m; z >= a[k]*v; z--){
38                             dp[z] = max(dp[z],dp[z-a[k]*v]+u*a[k]);
39                         }
40                     }
41                 }
42             }
43         }
44         printf("%d\n",dp[m]);
45     }
46     return 0;
47 }
View Code

 

posted @ 2014-10-01 14:16  狂徒归来  阅读(205)  评论(0编辑  收藏  举报