zoukankan      html  css  js  c++  java
  • hdu 5887 搜索+剪枝

    Herbs Gathering

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


    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
    /*
    hdu 5887 搜索+剪枝
    
    problem:
    01背包问题,只是里面的数据达到了1e9
    
    solve:
    先对所有的物品按贡献(w/v)进行排序, 当搜到第u个点时,第u+1个在剩下的贡献是最大的. 如果剩下空间的全部放
    u+1物品都无法大于当前的答案,则剪去.
    
    hhh-2016-09-20 20:25:42
    */
    #pragma comment(linker,"/STACK:124000000,124000000")
    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <map>
    #define lson  i<<1
    #define rson  i<<1|1
    #define ll long long
    #define clr(a,b) memset(a,b,sizeof(a))
    #define key_val ch[ch[root][1]][0]
    using namespace std;
    const int maxn = 200100;
    const int inf = 0x3f3f3f3f;
    const ll mod = 1e9 + 7;
    
    template<class T> void read(T&num)
    {
        char CH;
        bool F=false;
        for(CH=getchar(); CH<'0'||CH>'9'; F= CH=='-',CH=getchar());
        for(num=0; CH>='0'&&CH<='9'; num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p)
    {
        if(!p)
        {
            puts("0");
            return;
        }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    struct node
    {
        ll v,w;
    } p[maxn];
    
    bool cmp(node a,node b)
    {
        return 1.0*a.w/a.v > 1.0*b.w/b.v;
    }
    
    ll ans = 0,lm;
    int n;
    
    bool can_more(int u,ll val,ll &ans,ll limit)
    {
        ll t = limit/p[u+1].v + 1;
        if(val + p[u+1].w * t >= ans)
            return true;
        return false;
    }
    
    void dfs(int u,ll val,ll limit)
    {
        if(val > ans)
            ans = val;
        if(limit <= 0)
            return ;
        if(u < n && can_more(u,val,ans,limit))
        {
            for(int i = u+1; i <= n; i++)
            {
                if(limit >= p[i].v)
                    dfs(i,val + p[i].w,limit - p[i].v);
            }
        }
    }
    
    void init()
    {
    
    }
    
    int main()
    {
        init();
        while(scanf("%d%I64d",&n,&lm) != EOF)
        {
            for(int i =1; i <= n; i++)
            {
                read(p[i].v);
                read(p[i].w);
            }
            sort(p+1,p+n+1,cmp);
            ans =0 ;
            dfs(0,0,lm);
            print(ans);
        }
    }
    

      

  • 相关阅读:
    类的定义
    面向对象与面向过程介绍
    跨目录导入模块
    正则表达式re模块
    常用工具包(模块)
    生成器generator
    闭包
    命名空间name space
    函数的递归
    POJ1062
  • 原文地址:https://www.cnblogs.com/Przz/p/5890260.html
Copyright © 2011-2022 走看看