Bone Collector 分支限界

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?


Input The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone. Output One integer per line representing the maximum of the total value (this number will be less than 2 31). Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
#define happy

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define all(a) (a).begin(),(a).end()
#define pll pair<ll,ll>
#define vi vector<int>
#define P pair<int,int>
#define pb push_back
const int inf=0x3f3f3f3f;
ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const int N=1e3+10;
struct AA{
	ll v,w;//w weight
	bool operator<(const AA& b)const{
		return v*b.w>b.v*w;
	}
}a[N];
ll n;

double getsum(int i,ll m){
    double ans=0;
    while(m>0&&i<n) {
        if(m>a[i].w){
            ans+=a[i].v;
            m-=a[i].w;
            i++;
        }else{
            ans+=m*1.0*(1.0*a[i].v/a[i].w);
            break;
        }
    }
    return ans;
}

ll m,ans;

void dfs(int i,ll mnow,ll nans){
	//if(mnow<0)return;
	if(i==n){ans=max(ans,nans);return;}
	if(a[i].w<=mnow){
        dfs(i+1,mnow-a[i].w,nans+a[i].v);
        //if((double)a[i+1].v*mnow/a[i+1].w+nans<=ans)
        if(getsum(i+1,mnow)+nans<=ans)
			return;
        dfs(i+1,mnow,nans);
	}else if(getsum(i+1,mnow)+nans<=ans)
			return;
	dfs(i+1,mnow,nans);

}

int main(){
#ifdef happy
    freopen("in.txt","r",stdin);
#endif
	int t=rd();
	while(t--){
		n=rd(),m=rd();
		rep(i,0,n-1)
		a[i].v=rd();
		rep(i,0,n-1)
		a[i].w=rd();
		sort(a,a+n);
		ans=0;
		dfs(0,m,0);
		printf("%lld\n",ans);
	}
}


猜你喜欢

转载自blog.csdn.net/ujn20161222/article/details/80424850