Dfs+ Pruning
Some of the more powerful pruning, first of the value of the input order, if the minimum exceeds the limit, the direct output of 0, if the value of the largest limn add up no more than the limit, it also directly output the answer, the rest is the regular DFS.
#include <cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;Const intmaxn= -;intLimN;Long LongLIMV;intN;Long LongV[MAXN];Long Longans;voidDfsintNowintTotLong Longsum) { if(Sum>ans) ans=sum; if(now==n)return; if(tot+1>LIMN)return; if(SUM+V[NOW]>LIMV)return; DFS ( now+1, tot+1, sum+V[now]); DFS ( now+1, tot,sum);}intMain () { while(~SCANF ("%d%lld",&limn,&limv)) {scanf ("%d",&N); for(intI=0; i<n; i++) scanf ("%lld",&V[i]); Sort (v,v+N); Long Longk=0; for(intj=m-1; I>=n-min (limn,n); i--) k=k+V[i]; if(v[0]>limv) {printf ("0\n"); Continue; } if(k<=limv) {printf ("%lld\n", K); Continue; } ans=0; DFS (0,0,0); printf ("%lld\n", ans); } return 0;}
HDU 3448 Bag problem