HDU5887(SummerTrainingDay01-D)

Herbs Gathering

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1470    Accepted Submission(s): 366


Problem Description

Collecting one's own plants for use as herbal medicines is perhaps one of the most self-empowering things a person can do, as it implies that they have taken the time and effort to learn about the uses and virtues of the plant and how it might benefit them, how to identify it in its native habitat or how to cultivate it in a garden, and how to prepare it as medicine. It also implies that a person has chosen to take responsibility for their own health and well being, rather than entirely surrender that faculty to another. Consider several different herbs. Each of them has a certain time which needs to be gathered, to be prepared and to be processed. Meanwhile a detailed analysis presents scores as evaluations of each herbs. Our time is running out. The only goal is to maximize the sum of scores for herbs which we can get within a limited time.
 

Input

There are at most ten test cases.
For each case, the first line consists two integers, the total number of different herbs and the time limit.
The i-th line of the following n line consists two non-negative integers. The first one is the time we need to gather and prepare the i-th herb, and the second one is its score.

The total number of different herbs should be no more than 100. All of the other numbers read in are uniform random and should not be more than 109.
 

Output

For each test case, output an integer as the maximum sum of scores.
 

Sample Input

3 70
71 100
69 1
1 2
 

Sample Output

3
 

Source

 
大容量01背包,搜索加剪枝
 1 //2017-10-08
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 #define ll long long
 7 
 8 using namespace std;
 9 
10 const int N = 200;
11 
12 int n;
13 struct Bag{
14     ll v, w;
15     bool operator<(const Bag x) const {
16         return v*x.w > w*x.v;
17     }
18 }bag[N];
19 
20 ll ans, limit;
21 void dfs(int step, ll wight, ll value){
22     if(wight > limit)return;
23     if(value > ans)ans = value;
24     if(step >= n)return;
25     if(value*1.0 + bag[step].v*1.0/(bag[step].w*1.0)*(limit-wight) <= ans*1.0)
26       return;//如果以当前最高的性价比装满剩下的容量,所得价值都不如当前最优解,则剪枝。
27     dfs(step+1, wight+bag[step].w, value+bag[step].v);
28     dfs(step+1, wight, value);
29 }
30 
31 int main()
32 {
33     while(~scanf("%d%lld", &n, &limit)){
34         for(int i = 0; i < n; i++){
35             scanf("%lld%lld", &bag[i].w, &bag[i].v);
36         }
37         sort(bag, bag+n);
38         ans = 0;
39         dfs(0, 0, 0);
40         printf("%lld
", ans);
41     }
42 
43     return 0;
44 }
原文地址:https://www.cnblogs.com/Penn000/p/7637518.html