Title: http://poj.org/problem?id=1276
Multi-Backpack template problem, nothing to say, but must use the idea of binary system, otherwise it will time out, the idea of binary system in the previous blog has introduced, here is not more said.
#include <iostream>#include<string.h>#include<stdio.h>#include<algorithm>#include<math.h>using namespacestd;intv,n,w[ -],cnt[ -],dp[1000010];voidcomintv) { for(intI=v; i<=v; i++) {Dp[i]=max (dp[i],dp[i-v]+v); } return ;}voidOneintv) { for(inti=v;i>=v;i--) {Dp[i]=max (dp[i],dp[i-v]+v); } return ;}intMain () { while(SCANF ("%d", &v)! =EOF) {scanf ("%d",&N); for(intI=0; i<n; i++) {scanf ("%d%d",&cnt[i],&W[i]); } memset (DP,0,sizeof(DP)); for(intI=0; i<n; i++) { if(cnt[i]==0)Continue; if(cnt[i]*w[i]>=V) {COM (w[i]); } Else { intt=1; while(t<Cnt[i]) {One (W[i]*t); Cnt[i]-=T; T<<=1; } one (W[i]*Cnt[i]); }} printf ("%d\n", Dp[v]); } return 0;}
Poj1276:cash Machine (multiple backpacks)