Given a collection of items s={1,2,3,...,n}, the weight of item I is WI, its value is VI, the capacity of the backpack is w, that is, the maximum load weight does not exceed W. Within a limited total weight of W, how do we select items to make the total value of the item the most. If the item can not be divided, that is, the item I is either the entire selection, or not selected, can not put the item I in the backpack multiple times, and can not only load part I, then the problem is called 0-1 knapsack problem.
If the item can be split, then the problem is called knapsack problem, suitable for using greedy algorithm
Given n kinds of goods and a backpack. The weight of item I is WI, its value is VI, the capacity of the backpack is C. Q: How do I choose which items are loaded into my backpack so that the total value of the items loaded into the backpack is greatest?
(Y1,y2,..., yn) is an optimal solution for (3.4.1). Then (Y2,..., yn) is an optimal solution for the following sub-problems:
Proof: The use of the rebuttal method. If not, setting (Z2,z3,..., Zn) is an optimal solution to the sub-problem, and (Y2,y3,..., yn) is not the optimal solution. Apparently there's ∑v.IzI >∑vIyi (i=2,..., N) and w1y1+∑w izi<= C so v1y1+ ∑vizi (i=2,..., N) >∑v iyi, (I=1,..., N) description (y1,z2, Z3 ,..., Zn) is a better solution for (3.4.1) 0-1 knapsack problem, the derivation (Y1,y2,..., yn) is not the optimal solution of knapsack problem, contradiction.
Recursive relationship:
Set the sub-problem of the 0-1 knapsack problem
The optimal value is M (i,j), that is, M (i,j) is the backpack capacity of J, can choose the item for the I,i+1,...,n 0-1 knapsack problem optimal value. By the optimal substructure properties of the 0-1 knapsack problem, the recursive formula for calculating m (I,J) can be established:
Note: (3.4.3) This time the backpack capacity is J, can choose the item for I. At this point, after making a decision on XI, the problem is in one of two states: (1) The remaining capacity of the backpack is J, which has no benefit; (2) the residual capacity is J-WI, the benefit value increases VI ; (3) for the last item n, if J>=WN, it must be loaded to obtain Value vn; If 0<=j < Wn is not loaded, the value obtained is 0.
1#include <iostream>2#include <cstring>3 using namespacestd;4 5 #defineNUM 50//upper bound of number of items6 #defineCAP 1500//upper bound of backpack capacity7 intV[num];//the weight of the item8 intW[num];//the value of the item9 intP[NUM][CAP];//the array used for recursion. Ten //parameter C is the capacity of the backpack W,n is the number of items. One voidKnapsack (intCintN) A { - //Calculating recursive boundaries - intJmax=min (w[n]-1, c);//demarcation Point. the for(intj=0; j<=jmax; J + +) -p[n][j]=0; - for(intJ=w[n]; j<=c; J + +) -p[n][j]=V[n]; + for(inti=n-1; I>1; i--)//Calculate Recursive - { +Jmax=min (w[i]-1, c); A for(intj=0; j<=jmax; J + +) atp[i][j]=p[i+1][j]; - for(intJ=w[i]; j<=c; J + +) -P[i][j]=max (p[i+1][J], p[i+1][j-w[i]]+V[i]); - } -p[1][c]=p[2][C];//calculates the optimal value. - if(c>=w[1]) inp[1][c]=max (p[1][c], p[2][c-w[1]]+v[1]); - } to //the shape parameter group x is the solution vector. + voidTracebackintCintNintx[]) - { the for(intI=1; i<n; i++) * { $ if(p[i][c]==p[i+1][C]) x[i]=0; Panax Notoginseng Else{x[i]=1; c-=w[i];} - } theX[n]= (P[n][c])?1:0; + } A the intMain () + { - intX[num]; $ intW; $ intN; - while(SCANF ("%d", &w) &&W) - { thescanf"%d", &n); - for(intI=1; i<=n; i++)Wuyiscanf"%d%d", &w[i], &v[i]); theMemset (P,0,sizeof(P)); - knapsack (W, n); Wuprintf"%d\n", p[1][w]); - Traceback (W, n, x); About for(intI=1; i<=n; i++) $ if(X[i]) printf ("%d", i); -printf"\ n"); - } - return 0; A}
View Code
Dynamic Programming 0--1 knapsack problem