Poj 1384 piggy-Bank (full backpack)

Source: Internet
Author: User

Poj 1384 piggy-Bank (full backpack)

Http://poj.org/problem? Id = 1384

Question:

There are now N types of coins, each of which has a specific weight cost [I] grams and its corresponding value Val [I]. each coin can be used infinitely. it is known that the total weight of all coins in a piggy bank is exactly M grams. How many coins are the most valuable in this piggy bank? If M grams cannot exist, output "This is impossible .".

Analysis:

Because each coin can be used infinitely, it is obviously a complete backpack problem.

This questionRestrictions: The total weight of a coin is equal to M.

This questionTarget Condition: The total value of coins should be as small as possible.

So that DP [I] [J] = X indicates that only the first I currency is used, and the minimum value when the total weight reaches J grams is X.

Initialization: All DP values are Inf (infinite) and DP [0] = 0. (because the goal of this question is to minimize the value, the initialization is infinite. to maximize the target, initialize it to-1 .)

State Transfer: DP [I] [J] = min (DP [I-1] [J], DP [I] [J-cost [I] + val [I])

In the above equation, the former indicates that none of the I-th coins are selected, and the latter indicates that at least one I-th coin is selected.

The final request is DP [N] [M]. (If DP [N] [m] = inf, it indicates that M grams are inaccessibility.)

This project uses a rolling array, so DP only has the [J] dimension.

AC code:

# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 10000 + 5; # define INF 1e9int N; // currency type int m; // maximum weight int DP [maxn]; int cost [maxn]; // int Val [maxn] for each currency weight; // int main () {int t for each currency value; scanf ("% d", & T); While (t --) {// read data int M1, M2; scanf ("% d", & M1, & m2, & N); M = m2-m1; For (INT I = 1; I <= N; I ++) scanf ("% d ", & Val [I], & cost [I]); // initialize for (INT I = 0; I <= m; I ++) DP [I] = inf; DP [0] = 0; // recursive process for (INT I = 1; I <= N; I ++) {for (Int J = cost [I]; j <= m; j ++) DP [J] = min (DP [J], DP [J-cost [I] + val [I]);} // output result if (DP [m] = inf) printf ("This is impossible. \ n "); else printf (" the minimum amount of money in the piggy-bank is % d. \ n ", DP [m]);} return 0 ;}

Poj 1384 piggy-Bank (full 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.