HDU 5887 Herbs Gathering(背包问题的剪枝技巧)

 HDU 5887 Herbs Gathering(背包问题的剪枝技巧)_第1张图片

一个典型的背包问题,但是没办法开那么大的数组,我们先按照效率进行排序,利用搜索DFS但需要剪枝:

如果按照把剩余的物品尽量拿走(时间够则全拿,不够则按比例拿)其价值也不超过当前最优解则不需要继续深搜索

const int N=100+5;

    int n,m;
    double t; 
    int i,j,k;
    ll ans;
    struct Node
    {
        ll t,val;
        double rate;
        bool operator<(Node b){
            return rate>b.rate;
        }
    }a[N];
bool judge(int id,ll sum,int time)//剪枝判断
{
    for(int i=id;i=a[i].t){ time-=a[i].t; sum+=a[i].val; }
        else{ sum+=a[id].rate*(a[i].t-time); }
    }
    return sum>ans;
}
void DFS(int id,ll sum,int time)
//当前草药的编号,目前为止的价值 sum,剩余的时间
{
    if( !judge(id,sum,time) ) return  ;
    if(sum+a[id].rate*time>a[i].t>>a[i].val;
            a[i].rate=a[i].val*1.0/a[i].t;
        }
        sort(a,a+n);
        ans=0;
        DFS(0,0,m);
        pld(ans);
    }
    //PAUSE;
    return 0;
}

搞不懂那些大犇们整天都在想什么,用时间判断,不过确实快 ,快的离谱.....

 

const int N=100+5;

    int n,m;
    double t; 
    int i,j,k;
    ll ans;
    struct Node
    {
        ll t,val;
        double rate;
        bool operator<(Node b){
            return rate>b.rate;
        }
    }a[N];
void DFS(int id,ll sum,int time)
{
    if( (clock()-t)/CLOCKS_PER_SEC>0.02 ) return  ;
    if(sum+a[id].rate*time>a[i].t>>a[i].val;
            a[i].rate=a[i].val*1.0/a[i].t;
        }
        sort(a,a+n);
        ans=0;
        t=clock();
        DFS(0,0,m);
        pld(ans);
    }
    //PAUSE;
    return 0;
}

                              来自:【HDU 5887】Herbs Gathering(dfs+时间剪枝) 

你可能感兴趣的:(HDU,#,深搜的剪枝技巧)