Title Link: http://poj.org/problem?id=1276
Dynamic planning of knapsack problem
I hit a template on the backpack nine.
This problem is special in terms of weight and value, and the template is more general
Update the maximum value in the process of counting F
Complexity:V *σ (log n[i])
Note Initialization
#include <cstdio>#include<cstdlib>#include<ctime>#include<iostream>#include<cmath>#include<cstring>#include<algorithm>#include<stack>#include<Set>#include<queue>#include<vector>using namespacestd;Const intMAXN = -;Const intMAXC =100010;intF[MAXC];intN[MAXN], D[MAXN], W[MAXN];intc, N;intans;voidZeroonepack (intCostintweight) { for(inti = C; I >= cost; i--) {F[i]= Max (F[i], F[i-cost] +weight); if(F[i] >ans) ans=F[i]; }}voidCompletepack (intCostintweight) { for(inti = Cost; I <= C; i++) {F[i]= Max (F[i], F[i-cost] +weight); if(F[i] >ans) ans=F[i]; }}voidMultiplepack (intCostintWeightintamount) { if(Cost*amount >=c) {Completepack (cost, weight); return; } intK =1; while(K <amount) {Zeroonepack (k*cost, k*weight); Amount= Amount-K; K*=2; } zeroonepack (Amount*cost, amount*weight);}intMain () {//freopen ("In.txt", "R", stdin); while(SCANF ("%d%d", &c, &n) = =2) {ans=0; Memset (F,0,sizeof(f)); for(inti =0; i < N; i++) {scanf ("%d%d", &n[i], &D[i]); } for(inti =0; i < N; i++) {W[i]=D[i]; } for(inti =0; i < N; i++) Multiplepack (D[i], w[i], n[i]); printf ("%d\n", ans); } return 0;}
POJ 1276 Cash Machine knapsack problem