HDU5887 Herbs Gathering 搜索 + 剪枝

                                               Herbs Gathering

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


 

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

背包问题,但是由于时间和分数维范围太大,无法dp ,于是直接爆搜 + 剪枝

剪枝条件 

将所有药草按每分钟所得贡献从大到小排好 

如果用当前pos处的药草的单位时间贡献*剩余时间 + 前面所得累计贡献 < 目前最大贡献 则剪枝。

因为当前pos处药草的单位时间贡献比后面药草都要多,如果按当前都不行,后面肯定也不行。

 

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn =  110;
const int maxm = 1e6 + 100;
const int INF = 0x3f3f3f3f;
//const int mod = 1e9 + 7;
typedef pair P;
typedef long long LL;
const LL mod = 1e9 + 7;

#define PI 3.1415926
#define eps 1e-5
#define sc(x)  scanf("%d",&x)
#define pf(x)  printf("%d",x)
#define pfn(x) printf("%d\n",x)
#define pfs(x) printf("%d ",x)
#define pfln(x) printf("%I64d\n",x)
#define rep(i,a,n) for(int i = a; i < n; i++)
#define per(i,a,n) for(int i = n-1; i >= a; i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define pb(x)  push_back(x);

// const int BUF=40000000;
// char Buf[BUF], *buf=Buf;
// inline void read(int& a) {for(a=0;*buf<48;buf++); while(*buf>47) a=a*10+*buf++-48;}
//  fread(Buf,1,BUF,stdin);

void read(LL &x){
	char ch = getchar();x = 0;
	for (; ch < '0' || ch > '9'; ch = getchar());
	for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
}

int n;
LL T;
LL ans;
struct Node
{
  LL t;
  LL sc;
  bool operator < (const Node b)const
  {
    return this->t*b.sc < b.t*this->sc;
  }
}a[maxn];

void dfs(int pos, LL res, LL t)
{
  if(res > ans)  ans = res;
  if(pos >= n) return ;
  if(res + t * ((double)a[pos].sc/(double)a[pos].t) + eps < ans) return ;
  if(t >= a[pos].t)
     dfs(pos+1,res+a[pos].sc,t-a[pos].t);
  dfs(pos+1,res,t);
  return ;
}


int main()
{
  while(~scanf("%d%lld",&n,&T))
  {
    rep(i,0,n)  {read(a[i].t);read(a[i].sc);}
    sort(a,a+n);
    ans = 0;
    dfs(0,0LL,T);
    printf("%lld\n",ans);
  }
}

 

你可能感兴趣的:(基础算法)