Step by step (5) greedy (4) Some backpack Problems

Source: Internet
Author: User

Step by step (5) greedy (4) Some backpack Problems

Some knapsack problems are attributed to a type of knapsack problem, and most of them are the results of dynamic planning. However, the greedy algorithm solves some backpacks, whether it is ideological or operational, it is very simple.

First, let's take a look at what is called some backpacks.

There are N items, the weight of each item is WI, the price is: PI, there is a backpack, the maximum weight of M can be loaded.
Where (0 <= I <N, 0 <wi <. M ).
Q: How can I install a product that can be included in the package?Highest value(For each item, only part of the item can be loaded)

The following conditions are analyzed ::
1. The product weight is not infinite.

That means we can't just take one item. In most cases, we need to select multiple items.

2. items can be split into parts.

It means that we need to consider not only "Can this item be loaded "?, How much should we consider "? In both cases, we need to consider not the value of an item, but the cost effectiveness.

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

Based on these two attributes, we can calculate the product cost effectiveness, that is, the unit price.

4. The price of the items in the backpack is required.Highest

That is to say, we need to load the items we need in order of cost effectiveness from high to low until the backpack is full. Because it is selected in the order of cost-effectiveness from high to low, the value of this method for backpacks of the same weight is the biggest among all methods.

Therefore, we can write pseudo code to indicate the entire solution process:

Sort first sorts items by unit price from high to low for (loop each item) {if (the weight of the item is smaller than the remaining weight of the backpack) {remaining weight of the backpack-= total item weight; total value + = total item value;} else {total value + = item unit price * remaining weight of the backpack; exit cycle ;}}

Old Rules: see NYOJ 106

Backpack problem time limit: 3000 MS | memory limit: 65535 KB difficulty: 3

Description
Now there are a lot of items (they can be separated), and we know the value v and weight w of each item (1 <= v, w <= 10 ); if you have a backpack that can hold the weight of m (10 <= m <= 20), all you need to do is to pack the item into the backpack, this maximizes the total value of items in a backpack.

Input
Enter a positive integer n (1 <=n <= 5) In the first line, indicating that n groups of test data exist;
Then there is n test data. The first row of each group of test data has two positive integers s, m (1 <= s <= 10), and s indicates that there are s items. Each row in the next s line has two positive integers v, w.

Output
Outputs the value and value of the items in the backpack in each group of test data. Each output occupies one row.

Sample Input
1
3 15
5 10
2 8
3 9

Sample output
65

This is the same as the previous analysis process, and the unit price for this question is directly given, so it is more convenient to directly use the code:

/*********************************** Title: NYOJ 106-backpack problem *********************************** * Date: ************************************** author: liu Xu ************************************ Memory: 232 KBTime: 0 ms *************************************/ # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace std; # define MAX 105 struct Node {int w; int v ;}; Node map [MAX]; Bool cmp (Node a, Node B) {if (a. v! = B. v) {return. v> B. v;} return. w> B. w ;}int main () {int T = 0; scanf ("% d", & T); while (T --) {memset (map,-1, sizeof (map); int num = 0; int weight = 0; scanf ("% d", & num, & weight); for (int I = 0; I <num; I ++) {scanf ("% d", & map [I]. v, & map [I]. w);} sort (map, map + num, cmp); int ans = 0; for (int I = 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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.