Today, according to teacher Cui's knapsack problem nine to learn the multiple knapsack problem, the harvest is very big.
In the backpack problem nine lecture, Cui teacher altogether has given three kinds of complete knapsack problem solution, I have chosen the middle of the kind, that is, "into the 01 knapsack problem" solution, the backpack problem nine on the introduction of the very good, it is easy to understand, here is not much to say, but unfortunately the article did not give a complete code, posted here, The input and output formats are POJ1276:
#include <cstdio> #include <string.h> #include <algorithm> using namespace std;
const int vmax=100100,nmax=20000;
int V,n,nn,c[nmax],w[nmax],f[vmax];
int POW2 (int a) {int ret=1;
while (a--) ret*=2;
return ret;
int main () {int tmp1,tmp2,k,f;
while (scanf ("%d", &v)!=eof) {memset (f,0,sizeof (F));
nn=0;
scanf ("%d", &n);
for (int i=1;i<=n;i++) {scanf ("%d%d", &TMP1,&TMP2);
k=0;
while (1) {if (Tmp1-pow2 (k+1) +1<=0) break;
++k;
for (int j=0;j<k;j++) {f=pow2 (j);
++nn;
C[nn]=tmp2*f;
W[nn]=tmp2*f;
} ++nn;
F=tmp1-pow2 (k) +1;
C[nn]=tmp2*f;
W[nn]=tmp2*f;
for (int i=1;i<=nn;i++) for (int j=v;j>=c[i];j--) F[j]=max (F[j],f[j-c[i]]+w[i]);
printf ("%d\n", F[v]);
return 0; }