Dynamic planning-Multiple knapsack problems

Source: Internet
Author: User

0-1 knapsack problem

Complete backpack problem

The multi-pack problem is a complex of 0-1 knapsack problems and complete knapsack problems, which can be described as follows: from n items to a volume V backpack, where each item has a volume of W, a value of V, a quantity of k, and the maximum value of the loaded sum?

We know that the 0-1 knapsack problem is the basis of the knapsack problem, so when solving the multiple knapsack problem, the multi-pack will be converted to the 0-1 backpack. In the multi-pack problem, each item has k, you can think of each item as K, so you can use the 0-1 knapsack algorithm. However, this increases the size of the data. Because the algorithm has a time complexity of O (V*∑ni=1ki), reduce the number of each item KI.

Nine degree tutorial gives a method, the original number of items of K split into groups, each group can be regarded as a new item, its value and weight for all items in the reorganization of the value of the sum of weight, each group of items containing the number of original items are: 1, 2, 4...k-2^c+1, where C is to make k-2^c+ 1 The largest integer greater than 0. This greatly reduces the number of items, at the same time through the combination of these several original items to get a different combination of items can be 0 to K of any item between the value of weight and, so all these new items do 0-1 backpack, you can get multiple backpack solution. The time complexity after conversion is O (v*∑ni=1log2 (ki)).

An example of a nine-degree tutorial-cherish now, Thanksgiving life

Title Description: In order to save the lives of compatriots in the disaster area, you are prepared to purchase some food support disaster area, now suppose you have a total of funds n yuan, and the market has M rice, each kind of rice is bagged products, its price range, and can only buy the whole bag. Excuse me: How many kilograms of grain can you purchase with limited funds? Input: The input data first contains a positive integer c, which indicates a C set of test cases, the first line of each set of test cases is two integers n and m (1<=n<= -,1<=m<= -), which represents the amount of funds and the type of rice, then the M-row data, each containing 3 numbers p,h and C (1<=p<= -,1<=h<= $,1<=c<= -), respectively, the price per bag, the weight of each bag and the number of bags of the corresponding type of rice. Output: For each set of test data, output the maximum weight that can be purchased for rice, and you can assume that you will not be able to buy more than just all the rice, and you can afford to spend it. The output for each instance takes up one row. Sample input:18 22  - 44  - 2Sample output: -

Encoding implementation:

#include <stdio.h>structrice{intPrice ; intweight;} rice[2001]; intMaxintAintb) {    returna > B?a:b;} intdp_rice[101]; intMain () {intncase; scanf ("%d", &ncase);  while(ncase--)    {        intSummoney, Sperice; scanf ("%d%d", &summoney, &Sperice); intindex =0; intPR, Wei, num;  for(inti =1; I <= Sperice; i++) {scanf ("%d%d%d", &AMP;PR, &wei, &num); intK =1;  while(num-k>0) {num-=K; rice[++index].price = pr*K; Rice[index].weight= wei*K; K*=2; } rice[++index].weight = wei*num; Rice[index].price= pr*num; }          for(intj =0; J <= Summoney; J + +) Dp_rice[j]=0;  for(inti =1; I <= index;i++)         for(intj = Summoney; J >= Rice[i].price; j--) Dp_rice[j]= Max (dp_rice[j],dp_rice[j-rice[i].price]+rice[i].weight); printf ("%d\n", Dp_rice[summoney]); }    return 0; }/************************************************************** problem:1455 User: Ling Yueming Heart language:c++ Resul t:accepted time:10 Ms memory:1036 kb****************************************************************/

Dynamic planning-Multiple knapsack problems

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.