Hdu2159_fate [two-dimensional cost backpack] [full backpack]

Source: Internet
Author: User
Fate

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/others) total submission (s): 8112 accepted submission (s): 3776
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?
 
Input
There are multiple groups of input data. For each group of data, enter n, m, K, S (0 <n, m, K, S <100) four positive integers in the first row. 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)
 
Output
The maximum endurance that can be retained after the output reaches this level. If the output cannot reach this level, the output reaches-1.
 
Sample Input
10 10 1 10
1 1
10 10 1 9
1 1
9 10 2 10
1 1
2 2
 
Sample output
0
-1
1
 
Author
Xhd
 
Source

2008 Information Engineering College training team-trials


He has a patience when xhd is about to upgrade. Each monster has an experience and need

The endurance value consumed. Kill monsters to get the corresponding experience and consume the corresponding endurance value. Xhd can kill s at most

The upgrade requires n experience points. If his patience falls to 0, he will not play any more games.

Output the maximum endurance value that he can leave after upgrading this level. If it cannot be upgraded,-1 is output.

Idea: the things to be stored in a backpack must be experience, but the capacity of the backpack is not only the number of monsters, but also the Endurance

Durability. In this way, the number and patience of monsters are used as the two-dimensional capacity of the backpack. Use two loops to obtain s

Strange: the highest experience value obtained in the M capacity value.

Finally, traverse to find out the minimum patience consumed when the experience value is higher than the upgraded experience value.

Original patience-the patience consumed is the final result.

# Include <stdio. h> # include <string. h ># include <algorithm> using namespace STD; int V [110], W [110], DP [110] [2010]; int main () {int n, m, k, s; // n experience M endurance K strange species s maximum kill number while (~ Scanf ("% d", & N, & M, & K, & S) {for (INT I = 0; I <K; I ++) scanf ("% d", & V [I], & W [I]); // v [I] is the experience of killing monsters. W [I] is the endurance of killing monsters. memset (DP, 0, sizeof (DP )); for (INT I = 0; I <K; I ++) // The I th blame {for (Int J = 1; j <= s; j ++) // a maximum of J {for (int K = W [I]; k <= m; k ++) // endurance of consumption {DP [J] [k] = max (DP [J] [K], DP [J-1] [k-W [I] + V [I]); // DP [maximum number of kill monsters] [endurance consumed] = experience} If (DP [s] [m] <n) // kill S, and the endurance of M consumption cannot be upgraded. Printf ("-1 \ n"); else {int min = m; For (INT I = 0; I <= s; I ++) {for (Int J = m; j> = 0; j --) if (DP [I] [J]> = N & J <min) // find out the patience min = J;} printf ("% d \ n", M-min) that consumes the least effort to achieve the expected experience ); // maximum remaining endurance} return 0 ;}


Hdu2159_fate [two-dimensional cost backpack] [full backpack]

Related Article

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.