Dynamic Planning: backpack problems, dynamic planning

Source: Internet
Author: User

Dynamic Planning: backpack problems, dynamic planning
Backpack Problems

There are N items and a backpack with a capacity of V. The size of the I item is c [I], and the value is w [I]. Solving which items are loaded into a backpack can maximize the total value.

State transition equation:
F [I] [v] = max (f [I-1] [v], f [I-1] [v-c [I] + w [I])
This equation is very important. Basically, all the equations related to backpacks are derived from it.
The pseudo code is as follows:

for i=1..N     for v=V..0         f[v]=max{f[v],f[v-c[i]]+w[i]};

If the I-th item is not placed, the problem is converted to "before ". I−1 Item loading capacity is V ", Value: F [I − 1] [v] ;
If the I-th item is placed, the problem is converted to "before ". I−1 The remaining size of items is V−c [I] ", The biggest value you can get at this time is F [I − 1] [v − c [I] Add I Value of an item W [I] .

Example

Description
Chen is a talented child. His dream is to become the greatest physician in the world. To this end, he wants to worship the most prestigious physicians nearby. In order to judge his qualifications, the physician gave him a difficult problem. The doctor took him to a cave where herbs were everywhere and said to him, "Child, there are some different herbs in this cave. It takes some time to pick every plant, each strain also has its own value. I will give you some time, during which you can collect some herbs. If you are a smart child, you should be able to maximize the total value of collected herbs ."
If you are Chen, can you complete this task?

Input
The first line of the input has two integers T (1 <= T <= 1000) and M (1 <= M <= 100), which are separated by a space, T represents the total time that can be used for medicine collection, and M represents the number of herbs in the cave. The next line of M contains two integers ranging from 1 to 100 (including 1 and 100), indicating the time to pick a particular herb and the value of this herb, respectively.

Output
The output contains a row. This row contains only one integer, indicating the maximum total value of herbs that can be acquired within the specified time period.

Sample Input

70 371 10069 11 2

Sample Output

3
Solution code 1
# Include <iostream> using namespace std; int main () {int T, M; cin> T> M; int c [101] = {0 }; int w [101] = {0}; for (int I = 1; I <= M; I ++) {cin> w [I]> c [I];} int ** f = new int * [M + 1]; for (int I = 0; I <= M; I ++) {f [I] = new int [T + 1];} // The number of herbs is 0 for (int I = 0; I <= M; I ++) {f [I] [0] = 0;} // The collection time is 0 for (int I = 0; I <= T; I ++) {f [0] [I] = 0;} int I, j; for (I = 1; I <= M; I ++) {for (j = 1; j <= T; j ++) {if (j> = w [I]) {f [I] [j] = max (f [I-1] [j], f [I-1] [j-w [I] + c [I]);} else {f [I] [j] = f [I-1] [j] ;}} cout <f [I-1] [J-1] <endl; // cout <f [M] [T] <endl; for (int I = 0; I <= M; I ++) {delete [] f [I];} delete [] f; return 0 ;}
Solution Code 2
#include <iostream>using namespace std;int main(){    int T, M;    cin>>T>>M;    int c[101] = {0};    int w[101] = {0};    for (int i = 1; i <= M; i++)    {        cin>>w[i]>>c[i];    }    int f[1001][101] = {0};    for (int i = 1; i <= M; i++)    {        for (int j = 1; j <= T; j++)        {            if (j >= w[i])            {                f[i][j] = max(f[i - 1][j], f[i - 1][j - w[i]] + c[i]);            }            else            {                f[i][j] = f[i - 1][j];            }        }    }    cout<<f[M][T]<<endl;    return 0;}

Code 2 consumes a lot of space, but the code can be much simpler.

Solution code 3

You can use a one-dimensional array to solve this problem, but it is worth noting that the memory loop sequence must be V .. 0, that is:

for i=1..N     for v=V..0         f[v]=max{f[v],f[v-c[i]]+w[i]};

If the inner loop is 0. V, the actual state equation is:
F [I] [v] = max (f [I −1] [v], f [I] [v −c [I] + w [I])

#include <iostream>using namespace std;int main(){    int T, M;    cin>>T>>M;    int c[101] = {0};    int w[101] = {0};    for (int i = 1; i <= M; i++)    {        cin>>w[i]>>c[i];    }    int f[101] = {0};    for (int i = 1; i <= M; i++)    {        for (int j = T; j >= 1; j--)        {            if (j >= w[i])            {                f[j] = max(f[j], f[j - w[i]] + c[i]);            }        }    }    cout<<f[T]<<endl;    return 0;}

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.