Step by Step (v.) greedy (4) part knapsack problem

Source: Internet
Author: User

Part of the knapsack problem is attributed to the knapsack problem, and most of the knapsack problem is the result of dynamic programming, but the greedy algorithm to solve part of the backpack, whether it is thought or operation, are very simple.

First, let's take a look at what's called a partial backpack.

There are n items, the weight of each product is WI, the price is: PI, the existing one backpack, up to the weight of M.
Among them (0<=i< n,0< wi<. M).
Q: How to install the highest value of the goods loaded in the package (for each item can be installed only part of the product)

Let's analyze the conditions:
1. The product weight is not unlimited.

    那就说明我们不能只拿一种东西,大多数情况都要进行多个物品的选取。

2. Merchandise can be split into parts.

    那就说明我们需要考虑的不仅是“这个物品能不能装”?,还要考虑“装多少”?    在这两种情况下,我们需要考虑的是不能是物品的价值,而是性价比。

3. There are two attributes of the commodity: weight and price;

    根据这两个属性,我们可以计算出商品的性价比,也就是单价。

4. The most expensive items to load in the backpack

    那就是说,我们需要按照性价比从高到低的顺序进行,依次装入我们需要得物品,直到背包装满。    由于是按照性价比从高到低的顺序进行的选取,那么同样重量的背包,这种方法获得的价值是所有方法中最大的。

So we can probably write pseudo-code to represent the entire solution process:

sort 先把各个物品按照单价从高到低排序for(循环每一个物品){    if(物品的重量小于背包剩余的重量)    {        背包剩余的重量 -= 物品总重量;        总价值 += 物品总价值;    }else    {        总价值  += 物品单价*背包剩余重量;        退出循环;    }}

The usual, look at the title Nyoj 106

                            背包问题                时间限制:3000 ms  |  内存限制:65535 KB                            难度:3

Description
Now there are a lot of items (they are divisible), we know the value of each unit weight of each item V and the weight w (1<=v,w<=10); If you give you a backpack, it can hold a weight of M (10<=m<=20), all you have to do is pack the items in your backpack. , so that the total value of the items in the backpack is the largest.

input
The first line enters a positive integer n (1<=n<=5), which indicates that there are n sets of test data;
Then there are n test data, the first line of each set of test data has two positive integers s,m (1<=s<=10); s indicates that there is an S item. The next S-line has two positive integer v,w per line.

Output
Outputs the value of the items in the backpack for each set of test data and each output takes up one row.

Sample Input
1
3 15
5 10
2 8
3 9

Sample Output
65

and the previous analysis process, and this problem directly to the unit price, so more convenient, directly on the code:

/************************************* title:nyoj 106-Backpack problem ************************************ date:2015/07/ 23************************************ Author: Liu Xu ************************************memory:232kbtime:0ms******* ******************************/#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;#define MAXstructnode{intWintV;}; NodeMap[MAX];BOOLCMP (Node A,node b) {if(A.V! = B.V) {returnA.V > B.V; }returnA.W > B.W;}intMain () {intT =0;scanf("%d", &t); while(t--) {memset(Map, -1,sizeof(Map));intnum =0;intWeight =0;scanf("%d%d", &num, &weight); for(inti =0; i < num; i++) {scanf("%d%d", &Map[I].V, &Map[I].W]; } sort (Map,Map+num, CMP);intAns =0; for(inti =0; i < num; i++) {if(Weight >=Map[I].W) {Weight-=Map[I].W; Ans + =Map[I].V *Map[I].W; }Else{ans + =Map[I].V * weight; Break; }        }printf("%d\n", ans); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Step by Step (v.) greedy (4) part 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.