Retrospective method 01 Knapsack Problem __ Backtracking method

Source: Internet
Author: User
problem

01 knapsack problem before using dynamic programming method to achieve: Dynamic programming 01 knapsack problem (easy to understand) 01 knapsack problem general Use the subset tree frame of backtracking method to realize

#include <iostream> using namespace std; int n,c,bestp;//items number, backpack capacity, maximum value int p[10000],w[10000],x[10000],bestx[10000];//items value, weight of items, selection of goods//CP current total value,  
            CW currently put in total weight void backtrack (int i,int cp,int CW) {if (i>n) {if (CP&GT;BESTP) {  
            BESTP=CP;  
        for (i=1;i<=n;i++) bestx[i]=x[i];  
            If {//every layer of items can be selected to put or not put, 0 means not put, 1 means put in for (int j=0;j<=1;j++) {  
            X[i]=j;  
                if (cw+x[i]*w[i]<=c) {cw+=w[i]*x[i];  
                Cp+=p[i]*x[i];  
                Backtrack (I+1,CP,CW);  
                Cw-=w[i]*x[i];  
            Cp-=p[i]*x[i];   
    int main () {bestp=0;  
    cin>>n>>c;  
    for (int i=1;i<=n;i++) cin>>w[i];  
    for (int i=1;i<=n;i++) cin>>p[i];  
    Backtrack (1,0,0);  
    cout<<bestp<<endl;for (int i=1;i<=n;i++) cout << bestx[i] << ""; }

Input:
3 10
5 5 3
40 30 10

Output:
70
1 1 0
Process Analysis:
If you use an array to represent each recursive, the choice of each item, then: (0 for the election, 1 for the election)
(0,0,0) All BESTP = 0
(0,0,1) Select 3rd Item BESTP = 10
(0,1,0) .....
(0,1,1) .....
(1,0,0) .....
(1,0,1) .....
(1,1,0) Select 1th and 2nd, the weight is 10,bestp=70

(1,1,1) All options, weight of 13, cut off of course can also be used to arrange the tree to achieve

#include <iostream> using namespace std; int n, c, Bestp=0, cw=0, cp=0;  Number of items, backpack capacity, maximum value, current selected weight, currently selected value int w[10], v[10], x[10], bestx[10];//value of items, weight of items, selection of items int vis[10];//record whether int tol =
    0;//records recursive number of times void back (int cur) {tol++;
            if (cur > N) {if (cp > BESTP) {BESTP = CP;
            for (int i=1; i<=n; i++) {bestx[i] = X[i];
                }}else{for (int i=1; i<=n; i++) {if (!vis[i] && cw+w[i] <= c) {
                Vis[i] = 1;
                CW + = W[i];
                X[cur] = W[i];
                CP = V[i];

                Back (cur+1);
                Reset CW-= W[i];
                CP-= V[i]; 
                X[cur] = 0;
            Vis[i] = 0;
            } if (Cw+w[i] > C) {back (cur+1);
    int main () {CIN >> n >> C; for (int i = 1; I <= n; i++) {cin >>W[i];
    for (int i = 1; I <= n; i++) {cin >> v[i];
    } back (1);
    cout << BESTP << Endl;
    for (int i = 1; I <= n; i++) {cout << bestx[i] << "";
} cout << Endl << tol;  }

Input:
3 10
5 5 3
40 30 10

Output:
70
5 5 0

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.