"Greedy algorithm" knapsack problem

Source: Internet
Author: User

Title: There is a backpack, backpack capacity is m=150. There are 7 items that can be divided into any size.

It is required to maximize the total value of the items loaded into the backpack, but not to exceed the total capacity.

Item A B C D E F G

Weight 35 30 60 50 40 10 25

Value 10 40 30 50 35 40 30

Ideas:

Let you put items into the bag, the requirements of the total value of the items loaded into the package, to make the total value of the largest, you can think how to put one item to make the total value of the largest, so you can think of the following three ways to select items, that is, the possible local optimal solution :

①: Each time you choose the highest value to put in the bag.

②: Always choose the smallest weight to put in the bag.

③: Each time you choose the highest value of unit weight to put in the bag.

After finding the possible local solutions, the analysis of each solution can be combined into the overall optimal solution, and the above three partial solutions are analyzed:

①: Choose the highest value, you will ignore the weight, if

M=50,

Item 1: Weight: 50, Value: 40

Item 2: Weight: 20, Value 30

Item 3: Weight: 30, Value 30

Obviously, the local solution is not feasible for the above situation.

②: Choosing the least weight will ignore the value, similar to the ① strategy.

③: This strategy is always the most valuable item in the package, so this strategy is the right greedy strategy.

Note: (http://blog.csdn.net/a925907195/article/details/41314549 The blog says the third situation is wrong,

Item: A B C

Weight: 28 20 10

Value: 28 20 10

in fact, the situation is in line with the greedy strategy , because the total situation regardless of the first choice of two will be packed backpack, because the item can be divided into arbitrary size, so, even if the next time, you can also split the last item, put in, their unit weight value is the same, so, The last backpack has the same weight and the same weight as the same value. )

So using the third strategy, the code is as follows:

#include <iostream>#include<algorithm>using namespacestd;structbag{intweight; intvalue; floatbi; floatBili;} bags[ -];BOOLCompareConstBag &bag1,ConstBag &bag2);intMain () {intsum=0, N; floatM; intj=0; cout<<"Enter Backpack capacity and item type quantity:"<<Endl; CIN>>M>>N;  for(intI=0; i<n;i++) {cin>>bags[i].weight>>Bags[i].value; Bags[i].bi=bags[i].weight/Bags[i].value; }     for(intI=0; i<n;i++) {Bags[i].bili=0; } sort (Bags,bags+n,compare); for(j=0; j<n;j++){        if(bags[j].weight<=M) {Bags[j].bili=1; Sum+=Bags[j].weight; M-=Bags[j].weight; cout<<"Weight:"<<bags[j].weight<<"Value:"<<bags[j].value<<"items were put in a backpack"<<endl<<"Scale:"<<bags[j].bili<<Endl; }        Else  Break; }   if(j<N) {Bags[j].bili=m/Bags[j].weight; Sum+=bags[j].bili*Bags[j].weight; cout<<"Weight:"<<bags[j].weight<<"Value:"<<bags[j].value<<"was put in a backpack"<<endl<<"Scale:"<<bags[j].bili<<Endl; }    return 0;}BOOLCompareConstBag &bag1,ConstBag &bag2) {returnBag1.bi>Bag2.bi;}

C + + Knowledge points Summary:

① Use the sort () function to write at the beginning, #include <algorithm>

② using sort () to compare struct arrays:

BOOL Compare (const bag &BAG1,const bag &bag2) {return bag1.bi>bag2.bi;}   



"Greedy algorithm" knapsack problem

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.