0-1 Backpack Modified Version

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.