Hdu2159fate (two-dimensional backpack)

Source: Internet
Author: User
Fate

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3051 accepted submission (s): 1297

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

Problem description recently xhd is playing a game called fate. In order to get the best equipment, xhd is constantly killing monsters. Over time, xhd began to dislike monsters, but had to finish this last level by killing monsters. The problem now is that it still requires n experience to upgrade xhd to the last level. xhd also has m patience. Each time you kill a strange xhd, you will get experience, and reduce the patience. Xhd won't play this game when the level of patience falls below 0 or less. Xhd also said that he only kills s at most. Can he upgrade the last level?


There are multiple groups of input data. For each group of data, there are four positive integers (n, m, K, S (0 <n, m, K, S <100) in the first line. They indicate the expected experience values, the patience to be retained, the number of strange species, and the maximum number to kill monsters. Next, Enter K rows of data. Input two positive integers A and B (0 <a, B <20) for each row of data. They respectively indicate the experience and patience of killing a strange xhd. (There are countless monsters)


The maximum level of patience that can be retained after the output is upgraded. If the output level cannot be upgraded, the output level is-1.


Sample input10 10 1 101 110 10 1 91 19 10 2 101 12 2


The two-dimensional bag problem of sample Output0-11 refers to: for each item, there are two different kinds of charges; the choice of this item must be paid at the same time; for each generation


Each price has a maximum value (Backpack capacity) that can be paid ). Ask how to select an item to maximize the value. Set these two prices 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. Item Value

W [I].

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

The state transition equation is:

F [I] [V] [u] = max {f [I-1] [V] [u], f [I-1] [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 variable V and U 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.
For (I = 1; I <= m; I ++)
{
For (j = 1; j <= s; j ++)
For (k = 1; k <= K; k ++) if (I-B [k]> = 0)
{
If (d [I-B [k] [J-1] + A [k]> = d [I] [J])
D [I] [J] = d [I-B [k] [J-1] + A [k];
}

If (d [I] [s]> = N) break;
}

Hdu2159fate (two-dimensional 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.