# "Dynamic Planning" knapsack problem (i) 01 backpack full Backpack Multiple backpack

Source: Internet
Author: User

One, 01 backpack

There are n items and a backpack with a capacity of V. The price of item I (i.e. volume, hereinafter) is w[i], the value is c[i]. 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.

This is the most basic knapsack problem, the overall point is: choose or not, this is a question < (￣ˇ￣)/

Equivalent to F[i][j] represents the maximum value that can be gained from a backpack loaded with a capacity of V in the front I backpack.

For an item, there are only two cases

Situation one: I do not put in, then the value is: F[i-1][v]

case Two: The first piece is put in, the value is: F[i-1][v-c[i]]+w[i]

State transition equation:f[i][v] = max (F[i-1][v], f[i-1][v-w[i]]+c[i])

A bare 01 knapsack problem ↓_↓

Drug harvesting

Title Description Description

Chen Chen is a gifted child, his dream is to become the world's greatest physician. To this end, he wanted to worship the most prestigious physician in the vicinity as a teacher. In order to judge his qualifications, the physician gave him a difficult problem. The physician took him to a cave that was full of herbs, and said to him, "children, there are some different herbs in this cave, and it takes time to take each strain, and each plant has its own value." I will give you some time, during which time you can pick up some herbs. If you are a smart kid, you should be able to get the total value of the herbs that are picked up. ”

If you are Chen Chen, can you finish this task?

Enter a description Input Description

Enter the first line with two integers T (1<=t<=1000) and M (1<=m<=100), separated by a space, T represents the total amount of time that can be used to take the medicine, and M represents the number of herbs in the cave. The next M-line consists of two integers from 1 to 100 (including 1 and 100), each representing the time of picking a herbal herb and the value of the herb.

Output description Output Description

The output includes one row, which contains only an integer that represents the maximum total value of the herbal medicine that can be picked up within the prescribed time.

Sample input Sample Input

70 3

71 100

69 1

1 2

Sample output Sample Output

3

Data range and Tips Data Size & Hint

"Data Size"

For 30% of data,m<=10;

For all the data, m<=100.

`#include <stdio.h>#include<algorithm>using namespacestd;intf[1001][1001];intMain () {intT, n,c[10001], v[10001]; scanf ("%d%d", &t, &N);  for(inti =1; I <= N; i++) scanf ("%d%d", &v[i], &C[i]);  for(inti =1; I <= N; i++)     {          for(intj =0; J <= T; J + +) F[i][j]= f[i-1][j];  for(intj =0; J+v[i] <= T; J + +) F[i][j]= Max (F[i][j] + c[i], f[i-1][j+V[i]]); }     intAns =0;  for(inti =0; I <= T; i++) ans=Max (ans, f[n][i]); printf ("%d", ans); return 0; }`

You can also use a one-dimensional array to write ↓_↓

Set F[v] means that the weight does not exceed the maximum value of v kg, then F[v]=max (F[v],f[v-w[i]]+c[i]), when V>=w[i],1<=i<=n .

`#include <stdio.h>#include<algorithm>using namespacestd;Const intMAXM =2001, MAXN =101;intm, N;intW[MAXN], C[MAXN];intF[MAXM];intMain () {scanf ("%d%d", &m, &n);//backpack capacity m and number of items n     for(intI=1; I <= N; i++) scanf ("%d%d", &w[i],&c[i]);//weight and value of each item     for(intI=1; I <= N; i++)//set F (v) to indicate the maximum value of the weight not exceeding v kg         for(intv = m; V >= W[i]; v--)//note is reverse F[v]= Max (f[v-w[i]]+C[i], f[v]); printf ("%d\n", F[m]);//F (m) is the optimal solution    return 0;}`

Second, complete backpack

There are n items and a backpack with a capacity of V, with unlimited pieces available for each item. The cost of item I is w[i] and the value is c[i]. 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.

The full backpack is very similar to the 01 backpack, and the difference is that the complete backpack item has unlimited pieces. From the previous election or not choose to choose or not choose, choose a few. √

Like the 01 backpack, we can write the state transition equation:f[i][v]=max (f[i-1][v-k*c[i]]+k*w[i]|0<=k*c[i]<=v)

There is also a simple optimization ↓_↓

When the value of an item is less than the value of another item, but the price is higher than the other item, we can not consider the item. Even if two items I, J satisfies C[i]<=c[j] and w[i]>=w[j], the item j is removed without consideration. Why should we buy something that is expensive and disgusting (╯▽╰)

`#include <stdio.h>#include<algorithm>using namespacestd;Const intmaxm=2001, maxn=101;intn,m,v,i;intC[MAXN],W[MAXN];intF[MAXM];intMain () {scanf ("%d%d", &m,&n);//backpack capacity m and number of items n     for(i=1; i<=n;i++) scanf ("%d%d",&w[i],&C[i]);  for(i=1; i<=n;i++)         for(V=w[i]; v<=m; v++)//F[v] means the maximum value of the weight not exceeding v kg//here is the v++ order difference from 01 backpackF[v]=max (f[v-w[i]]+C[i], f[v]); printf ("%d\n", F[m]);//F[m] for the optimal solution    return 0;}`

Three, multiple backpack

There are n kinds of goods and a backpack with a capacity of V. (i) Items with a maximum of n[i] pieces available, each cost is w[i], the value is c[i]. 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.

There is one more restriction, and each item specifies the number of times available.

In the same vein, we can derive the state transition equation:f[i][v]=max (f[i-1][v-k*w[i]]+ k*c[i]|0<=k<=n[i])

An example ↓_↓

Celebration meeting

"Problem description"

In order to celebrate the class in the school sports meeting to obtain the first grade, the head teacher decided to open a celebration meeting, for this grant to buy prizes reward athletes. Expect the amount of money to buy prizes of the greatest value that can replenish their energy and stamina.

"Input Format"

The first line is two number n (n<=500), M (m<=6000), where n represents the number of prizes to be purchased, and M represents the amount of the grant. The next n lines, 3 numbers per line, V, W, S, respectively, indicate the price, value (price and value of the different concepts) and the quantity purchased (buy 0 to s pieces), where v<=100,w<=1000,s<=10.

"Output Format"

The first line: A number, indicating the maximum value this purchase can get (note!) Not the price).

"Input Sample"

5 1000

80 20 4

40 50 9

30 50 7

40 30 6

20 20 1

"Output Example"

1040

First, we give a naïve algorithm that is not optimized.

`#include <stdio.h>#include<algorithm>using namespacestd;intv[6002], w[6002], s[6002];intf[6002];intN, M;intMain () {scanf ("%d%d",&n,&m);  for(inti =1; I <= N; i++) scanf ("%d%d%d",&v[i],&w[i],&S[i]);  for(inti =1; I <= N; i++)        for(intj = m; J >=0; j--)           for(intK =0; K <= S[i]; k++)          {               if(j-k*v[i]<0) Break; F[J]= Max (f[j],f[j-k*v[i]]+k*W[i]); } printf ("%d\n", F[m]); return 0;} `

Binary optimized, converted to 01 backpack (split items)

`#include <stdio.h>#include<algorithm>using namespacestd;intv[10001],w[10001];intf[6001];intn,m,n1;intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++)    {        intx,y,s,t=1; scanf ("%d%d%d",&x,&y,&s);  while(s>=t) {v[++n1]=x*T; W[N1]=y*T; S-=T; T*=2; } v[++n1]=x*R; W[N1]=y*s;//divide s by the exponent of 2:1,2,4,...,2^ (k-1), s-2^k+1,    } for(intI=1; i<=n1;i++)         for(intj=m;j>=v[i];j--) F[j]=max (f[j],f[j-v[i]]+W[i]); printf ("%d\n", F[m]); return 0;}`

These are the three basic knapsack problems (*^__^*)

P.s. Here to beginner salted fish, if there are mistakes welcome you big guys pointed out ~

"Dynamic Planning" knapsack problem (i) 01 backpack full Backpack Multiple backpack

Related Keywords:

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.