首页 > ACM题库 > HDU-杭电 > HDU 3732-Ahui Writes Word-背包问题-[解题报告]HOJ
2015
02-21

HDU 3732-Ahui Writes Word-背包问题-[解题报告]HOJ

Ahui Writes Word

问题描述 :

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.

输入:

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)

输出:

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)

样例输入:

5 20
go 5 8
think 3 7
big 7 4
read 2 6
write 3 5

样例输出:

15
Hint
Input data is huge,please use “scanf(“%s”,s)”

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种类型的单词,
 
存在很多重复的,故可以转化成多重背包,然后加上二进制优化。
 
 #include <iostream>
 #include <cstdio>
 #include <cstring>
 #include <cmath>
 #include <algorithm>
 #include <climits>
 #include <vector>
 #include <queue>
 #include <cstdlib>
 #include <string>
 #include <set>
 #include <stack>
 #define LL long long
 #define pii pair<int,int>
 #define INF 0x3f3f3f3f
 using namespace std;
 int mp[12][12],dp[10010],a[20],n,m;
 int main() {
     char s[30];
     int u,v,tot;
     while(~scanf("%d %d",&n,&m)){
         memset(mp,0,sizeof(mp));
         for(int i = 0; i < n; i++){
             scanf("%s %d %d",s,&u,&v);
             mp[u][v]++;
         }
         memset(dp,0,sizeof(dp));
         for(u = 0; u < 11; u++){
             for(v = 0; v < 11; v++){
                 if(mp[u][v]){
                     int t = log2(mp[u][v]);
                     for(int k = 0; k <= t; k++){
                         if(k == t) a[k] = mp[u][v] - (1<<k) + 1;
                         else a[k] = 1<<k;
                     }
                     for(int k = 0; k <= t; k++){
                         for(int z = m; z >= a[k]*v; z--){
                             dp[z] = max(dp[z],dp[z-a[k]*v]+u*a[k]);
                         }
                     }
                 }
             }
         }
         printf("%d\n",dp[m]);
     }
     return 0;
 }

 

参考:http://www.cnblogs.com/crackpotisback/p/4003261.html