Hdoj 2602 Bone Collector "Dynamic Planning"

Source: Internet
Author: User

Topic Links:

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17434

Dynamic planning:

Classic 01 Knapsack problem

There are n items and a backpack with a capacity of V. The cost of article I is c[i], the value is w[i].
The sum of the values is maximized by solving which items are loaded into the backpack.

This is the most basic knapsack problem, characterized by: only one piece of each item, you can choose to put or not put.
Define the state with sub-question: F[i][v] Indicates the maximum value that the first I item can get in a backpack with a capacity of V. The state transfer equation is:
F[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}
Explain it in more detail:
"Put the first I item into a backpack with a capacity of V" This sub-problem, if only consider the article I item strategy (put or not), then can be converted into a only 1 items in the first I-related issues.
If I do not put the article I, then the problem is converted to "the first I-1 items into the capacity of the backpack", the value of f[i-1][v];
If you put the item I, then the problem is converted to "the first I-1 items into the remaining capacity of v-c[i] in the backpack", the maximum value can be obtained at this time is f[i-1][v-c[i]], plus the value obtained by placing the article I item w[i].

First consider how to achieve the basic idea above, there must be a main loop I=1..N, each time the two-dimensional array f[i][0..v] all values.
So, if you only use an array of f[0..v], can you guarantee that the f[v] in the end of the first cycle is the state f[i][v] that we define?
F[I][V] (F[i-1][v] and f[i-1] [V-c[i]] Two sub-problems are recursive, can be guaranteed in the push F[i][v] (also in the main cycle of the first time) can get f[v] and f[i-1][v] [f[i-1]] value?
In fact, this requires us to v=v in each main loop. 0 in the order of the push f[v], so as to ensure that push f[v] f[v-c[i]] saved is the value of state F[i-1][v-c[i]].
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]};
One of the F[v]=max{f[v],f[v-c[i]]} is just equivalent to our transfer equation F[i][v]=max{f[i-1][v],f[i-1][v-c[i]]}, because now F[v-c[i]] is equivalent to the original f[i-1][v-c [i]].

The last f[v] is the optimal solution to the problem.

1#include <cstdio>2#include <cstring>3 Const intMAXL = ++5;4 intMaxintAintb) {returnA>b?a:b;}5 intT, N, V, Value[maxl], Volume[maxl], DP[MAXL];6 7 intMain () {8scanf"%d", &t);9      while(t--){Tenscanf"%d%d", &n, &v); One          for(inti =1; I <= N; i++) scanf ("%d", &value[i]); A          for(inti =1; I <= N; i++) scanf ("%d", &volume[i]); -Memset (DP,0,sizeof(DP)); -          for(inti =1; I <= N; i++){ the              for(intj = V; J >= Volume[i]; j--) {//Note that the stop condition of J, one is to prevent cross-border, two is less than 0 after the answer is DP[J], has been calculated -                 //The inverse of the Traverse from V to 0 guarantees that Dp[j-volume[i]] is the result of the previous layer, -                 //and because the second dimension of the DP array involves only the previous layer of data, the data from the other layers is no longer accessible -                 //thus eliminating the first dimension of the DP array, each time the previous data is refreshed, saving space complexity to O (n); +DP[J] = max (Dp[j], dp[j-volume[i]]+value[i]); -             } +         } Aprintf"%d\n", Dp[v]); at     } -  -     return 0; -}

  

Hdoj 2602 Bone Collector "Dynamic Planning"

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.