[C++11] Algorithm [Exhaustive] output knapsack problem of all the satisfied solutions

Source: Internet
Author: User

On the question of knapsack problem, the previous statement is prepared, here only to discuss the realization

Input:

N

Ca

W_1 v_1

W_2 v_2

...

W_n V_n

where n is the total number of items, CA is backpack size, w_n is the weight of nth item, V_n is the value of nth item

Output:

V_1 x

V_2 x

V_3 x

...

Where V_n is the value of the backpack at the time of X, X is a sequence of 0, 1, indicating whether to put the backpack

Example: 1001 means the first and last item is put into the backpack, the middle two items are not put in

Requires writing a program that outputs all the solutions that can be satisfied.

The idea is simple, that is, poor lifting. Poor to lift every situation.

The pseudo code is as follows:

f () {  if  (nothing to put in the backpack) {    output   else  {    put in Backpack    f ()    not put in backpack    f () Restitution  }}

In this way, a binary tree is constructed that can output each of the optional solutions.

My code is as follows:

1#include <iostream>2#include <functional>3 4 structPack {5 unsigned cnt;6unsigned *w;//Weights7unsigned *v;//Values8unsigned *x;//put in or not9unsigned CA;//capacityTen    One Pack (unsigned items_cnt): CNT (items_cnt) { AW =Newunsigned [items_cnt]; -v =Newunsigned [items_cnt]; -x =Newunsigned [items_cnt]; the      -      for(inti =0; i < items_cnt; ++i) { -W[i] = V[i] = X[i] =-1; -     } +   } -    +~Pack () { A     Delete[] W; at     Delete[] v; -     Delete[] x; -   } - }; -  -  in intMain () { - unsigned C; to   +Std::cin >>C; -    the Pack P (c); *    $Std::cin >>p.ca;Panax Notoginseng    -    for(inti =0; i < p.cnt; ++i) { theStd::cin >> P.w[i] >>P.v[i]; +   } A   the   +   -   $Std::function<unsigned () > Totval = [&]() { $unsigned cnt =0; -      -      for(inti =0; i < p.ca; ++i) { the       if(P.x[i] = =1) CNT + =P.v[i]; -     }Wuyi      the     returnCNT; -   }; Wu    -Std::function<unsigned () > TOTWT = [&]() { Aboutunsigned cnt =0; $      -      for(inti =0; i < p.ca; ++i) { -       if(P.x[i] = =1) CNT + =P.w[i]; -     } A      +     returnCNT; the   }; -    $std::function<void() > Loop = [&]() { theunsigned no =-1; the      for(inti =0; i < p.cnt; ++i) { the       if(P.x[i] = =-1) { theNo =i; -          Break; in       } the     } the      About     if(No = =-1) { theStd::cout << totval () <<' '; the        for(inti =0; i < p.cnt; ++i) { theStd::cout <<P.x[i]; +       } -Std::cout <<Std::endl; the}Else {BayiP.x[no] =0; the loop (); the        -P.x[no] =1; -        the       if(TOTWT () <=p.ca) { the loop (); the       } the        -P.x[no] =-1; the     } the   }; the   94 loop (); the   the   return 0; the}

Test data:

5  the 9 8 1 2 3 8 1 0 3 9

Output:

0 000009 000010 000109 000118 00100 - 001018 00110 - 001112 01000 One 010012 01010 One 01011Ten 01100 + 01101Ten 01110 + 011118 10000 - 100018 10010 - 10011 - 10100 - 10101 - 10110Ten 11000 + 11001Ten 11010 + 11011 - 11100 - 11110

The optimal solution can be found, 10101.

[C++11] Algorithm [Exhaustive] output knapsack problem of all the satisfied solutions

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.