HDU 3448 Bag problem

Source: Internet
Author: User

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&GT;LIMN)return; if(SUM+V[NOW]&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.