"Backpack problem" 0-1 backpack, full backpack, multi-pack, mixed three kinds of backpack, two-bit cost backpack, group backpack

Source: Internet
Author: User

One, 0-1 knapsack problem

Input: The number of items in the first row N, the second line of backpack quality m, then the n row each row gives each item weight and value, each item only one.

Output: Maximum value the backpack can achieve

Sample input:

5 10

1 5

2 4

3 3

4 2

5 1

Sample output:

14

In the process of dynamic planning, you need to reverse the order, because if not reverse

When the i=0

f[0]=0;

F[1]=max (f[1],f[1-w[0]]+v[0]) = 5;

F[2]=max (f[2],f[2-w[0]]+v[0]) = 10;

F[3]=max (f[3],f[3-w[0]]+v[0]) = 15;

....

F[10]=max (f[10],f[10-w[0]]+v[0]) =50

Obviously not, because each item has only one, and the sequential traversal may use the case where the first item in the previous state has been placed

#include <iostream>#include<cstring>using namespacestd;intn,m,f[1111],w[1111],v[1111];//The total capacity of the M backpack, the volume of v items, the value of W itemsvoidOnezeropack (intMintVintW//0-1 Backpack{     for(inti=m;i>=v;i--) F[i]=max (f[i],f[i-v]+w);}intMain () { while(cin>>n>>m) { for(intI=0; i<n;i++) Cin>>v[i]>>W[i]; Memset (F,0,sizeof(f));  for(intI=0; i<n;i++) Onezeropack (M,v[i],w[i]); cout<<f[m]<<Endl; }    return 0;}

If you need to fully fill, then initialize the array, only f[0]=0 the rest of the assignment-∞

Second, complete knapsack problem

Input: The number of items in the first row N, the second line of backpack quality m, then the n row per row gives the weight and value of each item, each item has an unlimited number of.

Output: Maximum value the backpack can achieve

Sample input:

5 10

1 5

2 4

7 ·

4 2

5 1

Sample output:

50

If each infinite number of that should be sequential traversal, explained above ...

Code: Just change that on the basis above.

 for (int i=0; i<n;i++)  for (int j=w[i];j<=m;j++)        f[j]=max (F[j],f[j-w[i]]+v[i]);

Three, multiple backpack

Similar to a full backpack, there are num[i]+1 strategies for each item: 0 pieces, 1 pieces, 2 pieces ... num[i] pieces, there is a state transition equation: f[i][v] = Max{f[i−1][v−k∗ci] + k∗wi |0≤k≤mi}

Complexity is O (vσmi)

Binary optimization time complexity O (VNLOGM)

can be converted to 0-1 knapsack problem directly solved, the complexity is still O (vσmi). But we can reduce the complexity of time by optimizing the binary idea. The item I is divided into several items, each of which has a factor, and the cost and value of the item are the original cost and value multiplied by this factor. Make these coefficients 1,2,4,...,2^ (k-1), n[i]-2^k+1, and K is the largest integer that satisfies the n[i]-2^k+1>0. For example, if N[i] is 13, the items are divided into four items with coefficients of 1,2,4,6 respectively.

Original title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2844

Problem Solving Report: http://www.cnblogs.com/asuml/p/5730400.html

Four, mixed three kinds of backpack

Very well understood to enclose the pseudo-code directly:

For I=0 to N-1

If I is 0-1 backpack

Onezeropack (int m,int v,int W)

Else If I is a full backpack

Completepack (int m,int v,int W)

Else

Multiplepack (int m,int v,int w,int num)

Backpack for five or two-dimensional fee

Add one dimension to the one-dimensional state corresponding to the cost.

Three-dimensional transfer equation: f[i,v,u] = Max{f[i−1,v,u],f[i−1,v−ci,u−di] + Wi}

Two-dimensional transfer equation: F[v,u]=max{f[v,u],f[v−ci,u−di] + Wi}

Six, the knapsack problem of the group

1. Problem

There are n items and a backpack with a capacity of V. The cost of article I is CI, the value is WI. These items are divided into K-groups, each of which conflicts with each other, with a maximum of one item selected. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.

2. Transfer equation:

F[K,V] = Max{f[k−1,v],f[k−1,v−ci] + Wi |item i∈group k}

3. Pseudo-code:

For K=1 to K

For V=v to 0

For all I belong to Group K

F[v]=max{f[v],f[v-c[i]]+a[i]

Original title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1712

Problem Solving Report: http://www.cnblogs.com/asuml/p/5732073.html

"Backpack problem" 0-1 backpack, full backpack, multi-pack, mixed three kinds of backpack, two-bit cost backpack, group backpack

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.