HDU 2159 fate (two-dimensional cost backpack, template question)

Source: Internet
Author: User

 

Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2159

 

The first two-dimensional cost backpack, I still feel very tricky. Later I looked at the materials and added one dimension to the one-dimensional model, which is actually composed of two 001 backpacks.

For more information, see:

 

 

The two-dimensional bag problem refers to: for each item, there are two different charges; the two costs must be paid at the same time when this item is selected; there is a maximum charge (Backpack capacity) for each price ). Ask how to select an item to maximize the value. Set these two costs to price 1 and price 2, respectively. The two costs required for item I are a [I] and B [I]. The maximum value (two types of backpack capacity) can be paid at two costs: V and U. The value of an item is W [I].

The fee is added to one dimension. You only need to add one dimension to the status. If f [v] [u] is set, it indicates the maximum value that can be obtained when the cost of the first I item is V and U respectively. The state transition equation is:

f[v][u]=max{f[i-1][v][u],f[v-a[i]][u-b[i]]+w[i]}

As mentioned above, only two-dimensional arrays can be used: when each item can only be retrieved once, the V and U variables adopt a backward loop, when an item is like a full backpack, use a sequential loop. Split an item when there are multiple backpack problems.

 

 

 

You may have some ideas here, so you should be able to understand the two-dimensional cost backpack with a look at the code.

First, let's look at the complete backpack template:

Void completepack (INT value, int weight)
{
Int I;
For (I = weight; I <= V; I ++)
DP [I] = max (DP [I], DP [I-weight] + value );
 
}

What is stored in DP is generally the maximum value or the best value we need, the subscript of DP, etc., which is generally used to record the constraints for us.

This question requires three aspects: Experience, patience, and the number of monsters to be killed.

It is easy to see that experience is the most demanding. The conditions for patience and monsters are our restrictions.

It is worth noting that the kill is once, so the subscript in the full template only needs to be-1 each time.

Therefore, the dynamic equation for this question should be:

DP [J] [k] = max (DP [J] [K], DP [J-tired [I] [k-1] + exp [I]);

 

 

The AC code is as follows (some instructions are added for this question ):

 

 

# Include <iostream> # include <stdio. h> using namespace STD; int DP [1000] [1000]; int exp [1000], tired [1000]; int need, patience, kind, kill; int max (int A, int B) {If (A> B) return a; return B;} int main () {While (scanf ("% d", & need, & patience, & kind, & kill )! = EOF) {int I, j, k; memset (DP, 0, sizeof (DP); for (I = 0; I <kind; I ++) scanf ("% d", & exp [I], & tired [I]); // experience, fatigue for (I = 0; I <kind; I ++) // n for (j = tired [I]; j <= patience; j ++) // full backpack, fatigue for (k = 1; k <= kill; k ++) // Number of kill monsters {DP [J] [k] = max (DP [J] [K], DP [J-tired [I] [k-1] + exp [I]); // DP stores experience values} int flag = 0; int temp; for (I = 0; I <= patience; I ++) // search for the first value in DP that is greater than the expected value (the value in the future must be larger, but the fatigue will decrease) {If (flag = 1) break; For (j = 0; j <= kill; j ++) {If (DP [I] [J]> = need) {// t EMP = DP [I] [J]; The experience is actually used .. I can output the answer. The HDU sample data is not covered with temp = I; // here I represents the fatigue flag = 1; break ;}}if (flag = 1) printf ("% d \ n", patience-Temp); elseprintf ("-1 \ n") ;}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.