#include <cstdio> #include <cstring>using namespace std;const int maxn = 200;template <class type>void T Raceback (int n,type w[],type v[],type p[maxn][maxn],int *head,int x[]); template <class type>type Knapsack (int n, Type c,type v[],type w[],type p[maxn][maxn],int x[]) {int *head = new Int[n+2]; HEAD[N+1] = 0,p[0][0] = 0,p[0][1] = 0; int left = 0,right = 0,next = 1; Head[n] = 1; for (int i=n, i>=1; i--) {int k = left; for (int j = left; j<=right; J + +) {if (p[j][0] + w[i]>c) break; Type y= p[j][0]+w[i],m = p[j][1] + v[i]; while (k<=right&&p[k][0]<y) {p[next][0] = p[k][0]; P[NEXT++][1] = p[k++][1]; } if (k<=right&&p[k][0]==y) {if (m<p[k][1]) m = p[k][1]; k++; } if (M>p[next-1][1]) {p[next][0]=y; P[next++][1] = m; } while (k<=right&&p[k][1]<=p[next-1][1]) k++; } while (K<=right) {p[next][0] = p[k][0]; P[NEXT++][1] =p[k++][1]; } left = Right+1,right = Next-1,head[i-1] =next; } traceback<type> (n,w,v,p,head,x); return p[next-1][1];} Template <class type>void Traceback (int n,type w[],type v[],type p[maxn][maxn],int head[],int x[]) {Type J = P[he Ad[0]-1][0],m= P[head[0]-1][1]; for (int i=1; i<=n; i++) {x[i] = 0; for (int k = head[i+1]; k<=head[i]-1; k++) {if (p[k][0]+w[i]==j&&p[k][1]+v[i]==m) { X[i] = 1; j = p[k][0]; m = p[k][1]; Break }}}}int N,c,w[maxn],v[maxn],p[maxn][maxn],x[maxn];int main () {scanf ("%d%d", &n,&c); for (int i=1; i<=n; i++) scanf ("%d", w+i); for (int i=1; i<=n; i++) SCANF ("%d", v+i); printf ("%d\n",knapsack<int> (n,c,v,w,p,x));} /*5 102 2 6 5 46 3 5 4 6*/
Textbook 75 pages, textbook grammatical errors have been corrected.
0-1 Backpack Modified Version