您的位置:首页 > 其它

POJ 2392 Space Elevator (多重背包问题)

2013-04-10 21:01 477 查看
Space Elevator

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6714 Accepted: 3116
Description
The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <= 400) different types of blocks with which to build the tower. Each block of type i has height h_i (1 <= h_i <= 100) and is available in quantity c_i (1 <= c_i <= 10). Due to possible damage caused by cosmic rays, no part of a block of type i can exceed a maximum altitude a_i (1 <= a_i <= 40000). 

Help the cows build the tallest space elevator possible by stacking blocks on top of each other according to the rules.
Input
* Line 1: A single integer, K 

* Lines 2..K+1: Each line contains three space-separated integers: h_i, a_i, and c_i. Line i+1 describes block type i.
Output
* Line 1: A single integer H, the maximum height of a tower that can be built
Sample Input
3
7 40 3
5 23 8
2 52 6

Sample Output
48

思路:因为每种类型的砖头都有一定的高度限制,这样只要根据每种砖头的高度限制进行升序排序,把限制当作背包容量,分别进行多重背包。





View Code

1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <algorithm>
5 #define MAX 40005
6
7 using namespace std;
8
9 struct node
10 {
11     int height,limit,num;
12 };
13
14 node block[405];
15 int dp[MAX];
16 int K;
17
18 bool cmp(node a,node b)
19 {
20     return a.limit < b.limit;
21 }
22
23 void multiPack(int ht,int n,int lmt)
24 {
25     if(ht * n > lmt)
26     {
27         for(int i=ht; i<=lmt; i++)
28             dp[i] = max(dp[i],dp[i-ht]+ht);
29     }
30     else
31     {
32         int k = 1;
33         int num = n;
34         while(k < num)
35         {
36             for(int i=lmt; i>=ht*k; i--)
37                 dp[i] = max(dp[i],dp[i-k*ht]+k*ht);
38             num -= k;
39             k *= 2;
40         }
41         for(int i=lmt; i>=ht*num; i--)
42             dp[i] = max(dp[i],dp[i-num*ht]+num*ht);
43     }
44 }
45
46 int main()
47 {
48     scanf("%d",&K);
49     for(int i=1; i<=K; i++)
50         scanf("%d%d%d",&block[i].height,&block[i].limit,&block[i].num);
51     sort(block+1,block+K+1,cmp);
52     memset(dp,0,sizeof(dp));
53     for(int i=1; i<=K; i++)
54         multiPack(block[i].height,block[i].num,block[i].limit);
55     int ans = 0;
56     for(int i=1; i<=block[K].limit; i++)
57         if(ans < dp[i])
58             ans = dp[i];
59     printf("%d\n",ans);
60     return 0;
61 }


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: