hdu5887Herbs Gathering【大数据01背包:搜索后缀+剪枝】

41 篇文章 1 订阅
33 篇文章 2 订阅

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背包,也不是我想象中的贪心,而是搜索,不仅要排序,还要后缀优化,尼玛,都没听说过啊,要不是暑假做01背包的课件,boss当时说了有递推和递归,我都不知道可以写成搜索……

//358MS	1788K	1372B	C++  2016-09-20 11:12:56
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    long long value,cost;
}num[120];
bool cmp(node x,node y)
{
    return x.cost>y.cost;
}
long long numvalue[120],numcost[120];
int n;
long long sum,tot;
void dfs(int x,long long costsum,long long valuesum)
{
    //printf("x=%d,costsum=%lld,valuesum=%lld\n",x,costsum,valuesum);
    if(x==n+1)
    {
        if(valuesum>tot)tot=valuesum;
        return;
    }
    if(valuesum+numvalue[x]<=tot)return;
    if(costsum+numcost[x]<=sum)
    {
        if(valuesum+numvalue[x]>tot)tot=valuesum+numvalue[x];
     //   printf("tot=%lld\n",tot);
        return;
    }
    if(costsum+num[x].cost<=sum)
        dfs(x+1,costsum+num[x].cost,valuesum+num[x].value);
    dfs(x+1,costsum,valuesum);
}
int main()
{
  //  freopen("cin.txt","r",stdin);
    while(~scanf("%d%lld",&n,&sum))
    {
        for(int i=1;i<=n;i++)scanf("%lld%lld",&num[i].cost,&num[i].value);
        sort(num+1,num+1+n,cmp);
        numvalue[n+1]=numcost[n+1]=0;
        for(int i=n;i>=1;i--)numvalue[i]=numvalue[i+1]+num[i].value,numcost[i]=numcost[i+1]+num[i].cost;
       // for(int i=1;i<=n;i++)printf("i=%d,value=%lld,cost=%lld\n",i,numvalue[i],numcost[i]);
        tot=0LL;
        dfs(1,0LL,0LL);
        cout<<tot<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值