#include <cstdio> #include <cstring> using namespace std; const int maxn = 200; template <class Type> void Traceback(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[head[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 10 2 2 6 5 4 6 3 5 4 6 */
课本75页,课本语法错误已修正。
时间: 2024-10-16 03:06:29