Http://www.cnblogs.com/kuangbin/archive/2012/09/14/2685430.html
#include <iostream>#include<string>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>#include<stack>#include<queue>#include<cctype>#include<vector>#include<iterator>#include<Set>#include<map>#include<sstream>using namespacestd;#defineMem (A, B) memset (A,b,sizeof (a))#definePF printf#defineSF scanf#defineSPF sprintf#definePB Push_back#defineDebug printf ("!\n")#defineMAXN 1010#defineMAX (A, b) a>b?a:b#defineBlank pf ("\ n")#defineLL Long Long#defineAll (x) X.begin (), X.end ()#defineINS (x) Inserter (X,x.begin ())#definePqueue priority_queue#defineINF 0x3f3f3f3fintn,c1,c2;BOOLvis[1030];intw[ the],dp[1030];intstate[1030];BOOLJudgeintx) {mem (Vis,false); vis[0] =true; inti,j,sum=0; for(i=0; i<n;i++) { if((1<<i) &x) {sum+=W[i]; for(j=c1;j>=w[i];j--) { //record all possible weight values in the C1 if(vis[j-W[i]]) VIS[J]=true; } } } if(SUM>C1+C2)return false; for(i=0; i<=c1;i++) { //Judging If you can accept this value in C2. if(Vis[i] && sum-i<=C2)return true; } return false;}intMain () {intt,i,j; SF ("%d",&t); intIcase=0; while(t--) {mem (W,0); Icase++; SF ("%d%d%d",&n,&c1,&C2); for(i =0; i<n;i++) {SF ("%d",&W[i]); } inttot =0; for(i=0;i< (1<<n); i++) dp[i]=INF; dp[0] =0; for(i=0;i< (1<<N); i++) { if(judge (i)) {State[tot++] =i; } } for(i=0; i<tot;i++) { for(J= (1<<n)-1; j>=0; j--) { if(Dp[j]==inf)Continue; if((j&state[i]) = =0) {dp[j|state[i]] = min (dp[j]+1, dp[j|State[i]]); }}} PF ("Scenario #%d:\n%d\n\n", icase,dp[(1<<n)-1]); } return 0;}
POJ 2923 (State collection Backpack)