Poj 1252 euro efficiency (BFS or full backpack)

Source: Internet
Author: User

Poj 1252 euro efficiency (BFS or full backpack)

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

Question:

There are 6 currencies in which their nominal values are 1, V2, V3, V4, V5, and v6, and their maximum values are less than 100. the minimum value is always 1 (that is, V1 ). the problem now is that you need to use up the six currencies in a small amount.StructureThe amount of money with a nominal value of 1 to 100. How many currencies do you need to construct an average number of these 100 currencies? How many currencies are required at most?

Note: The components here include not only addition, but also subtraction. assume that the nominal value of the currency is 1, 3, 5, 7, 9, and 11. You need to construct 4. then you can use 1 + 3, or 5-1 to construct it. You can also use 1 + 1 + 1 + 1 to construct it.

Analysis:

Solution 1: BFS breadth-first search.

Because we need to construct 1-100 and 100 numbers, we can ask each number that requires at least a few currencies. in addition, the original currency can be reduced, so we can add another six currencies based on the original six currencies. the area of these six new currencies is the negative value of the currency's nominal value in the original six.

We make Dist [I] = x to indicate that constructing a nominal value of I requires at least X currencies.

The initial dist is full INF. and DIST [0] = 0.

Then we can proceed to BFs.The general idea isStarting from the u point, if the distance between the U point and the V point is smaller than the original distance of the V point, we can find a better construction method of the V point. all other vertices constructed by vertices also need to be updated, so vertices enter the queue. The next time you retrieve vertices and perform BFs, you can update the DIST values of all other successor nodes constructed by vertices.

When the queue is empty, it indicates the construction is complete. Exit the statistical result.

Note that: Since the nominal value of each plus currency can be positive or negative, even if we only need to construct money within the nominal value of 100, it may exceed 100 in the process of constructing 100. for example, when the currency nominal value is 1 51 91 92 93 94, the optimal method for constructing 100 is 51 + 51-1-1. in this case, we need to construct Dist [102] with a nominal value of 102 yuan to correctly launch Dist [100]. (Think about it?So the BFS range is not 1-100, but should be 1 to a greater value, but the size of this value must be proven strictly. 200 I used in program implementation can get the correct result.

 

Solution 2: dual full backpack DP.

Because money with a nominal value needs to be added or subtracted from the monetary value, the following structure is required:

100 = 55 + 55-4-4-2 we can define 55 + 55Max payment count,-4-4-2 isNumber of zeros, 100 isFinal payment count.

In fact, we may divide each of the above constructor into two parts: Constructing the maximum payment amount and finding zero.

The first step is to construct the maximum number of payments: (a full backpack process)

When DP [I] [J] = X represents money that can be obtained with the first I currency, it requires at least X currency.

The initial Dp value is Inf (infinite), and DP [0] [0] = 0.

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

The former indicates that no I-th currency is used, and the latter indicates that at least 1 I-th currency is used.

In the end, DP [N] [0] to DP [N] [maxn] are all the data we need. they indicate the minimum number of currencies required to reach the monetary nominal value when we only increase or decrease.

Step two :(Another complete backpack Process)

For the DP result inherited from the previous step, we can now set DP [I] [J] = x to indicate that after I make the decision, it indicates that, based on the current currency value minus Val [I], if the monetary nominal value is J, at least X currencies are required.

Initial Value: The result of the previous DP.

Status transfer: DP [I] [J-Val [I] = min (DP [I-1] [J-Val [I], DP [I] [J] + 1)

The former indicates that the first currency is not reduced, and the latter indicates that at least one second currency is subtracted.

In the end, DP [N] [1] to DP [N] [100] are all the data we need.

The program uses a rolling array, so DP only has the [J] dimension.

 

Solution 3: it is regarded as 12 currencies and a full backpack once

In addition, can we regard 6 currencies as 12 different currencies and then perform a full backpack?

Yes, but the recurrence order of the plus and minus currencies is different. The plus currencies are from the smallest value to the largest, while the minus currencies are from the largest to the smallest. different Val [I] can be processed.

I tested the maxn in the program myself, but someone on the Internet gave a proof of the maxn range:

"The maximum number of payments (for example, if you pay 49 in RMB, you can pay 50 First and retrieve 1, then 50 is called the maximum number of payments) must be less than (100/Val [1]) * val [6] + 100, because if this value is reached, the value must be at least 100/Val [1] = 100 to return to the range of [1,100, it is better to directly use Val [1] for payment"

AC code 1: BFS practices

 

# Include <cstdio> # include <cstring> # include <algorithm> # include <queue> using namespace STD; # define INF 1e8const int maxn = 200 + 5; int n = 12; // plus and minus 12 currencies int Val [12 + 5]; // The value of each currency int Dist [maxn]; void BFS () {// initialize queue <int> q; For (INT I = 1; I <maxn; I ++) Dist [I] = inf; Dist [0] = 0; q. push (0); // BFS traverses all nodes in 1-and finds the minimum value of each Dist [I] While (! Q. empty () {int u = Q. front (); q. pop (); For (INT I = 1; I <= N; I ++) {int v = u + val [I]; if (V> = 1 & V <maxn & Dist [u] + 1 <Dist [v]) {Dist [v] = DIST [u] + 1; q. push (v) ;}}// statistical result: sum is the total number, max_step is the maximum number int sum = 0, max_step = 0; For (INT I = 1; I <= 100; I ++) {max_step = max (max_step, DIST [I]); sum + = DIST [I];} printf ("%. 2f % d \ n ", sum/100.0, max_step); // if it is changed to %. 2 lf submit, then C ++ AC, G ++ wa .} int main () {int t; scanf ("% d", & T); While (t --) {for (INT I = 1; I <= 6; I ++) scanf ("% d", & Val [I]); For (INT I = 7; I <= N; I ++) val [I] =-Val [i-6]; BFS (); // handle the problem} return 0 ;}

AC code 2: 2 full backpacks

 

# Include <cstring> # include <cstdio> # include <algorithm> # define INF 1e9using namespace STD; const int maxn = 2000 + 5; int n = 6; // Number of currency types int Val [7]; // currency face value int DP [maxn]; int main () {int t; scanf ("% d", & T ); while (t --) {// read input data for (INT I = 1; I <= 6; I ++) scanf ("% d ", & Val [I]); // initialize for (INT I = 0; I <maxn; I ++) DP [I] = inf; DP [0] = 0; // forward to DP, update DP [maximum payment amount] for (INT I = 1; I <= N; I ++) {for (Int J = Val [I]; j <maxn; j ++) DP [J] = min (DP [J], DP [J-Val [I] + 1);} // reverse DP, update DP [Final payment count] For (INT I = 1; I <= N; I ++) {for (Int J = maxn-1; j> = Val [I]; J --) DP [J-Val [I] = min (DP [J-Val [I], DP [J] + 1 );} // output int sum = 0, max_val = 0; For (INT I = 1; I <= 100; I ++) {sum + = DP [I]; max_val = max (max_val, DP [I]);} printf ("%. 2f % d \ n ", sum/100.0, max_val );}}

AC with code 3: A full backpack

# Include <cstring> # include <cstdio> # include <algorithm> # define INF 1e9using namespace STD; const int maxn = 2000 + 5; int n = 12; // Number of currency types int Val [13]; // currency face value int DP [maxn + 100]; int main () {int t; scanf ("% d ", & T); While (t --) {// read input data, construct 12 currencies for (INT I = 1; I <= 6; I ++) {scanf ("% d", & Val [I]); Val [I + 6] =-Val [I] ;}// initialize for (INT I = 0; I <maxn; I ++) DP [I] = inf; DP [0] = 0; // recursive process for (INT I = 1; I <= N; I ++) {If (Val [I]> 0) {for (Int J = Val [I]; j <maxn; j ++) DP [J] = min (DP [J], DP [J-Val [I] + 1);} else if (Val [I] <0) {int v =-Val [I]; for (Int J = maxn-1; j> = V; j --) DP [J-v] = min (DP [J-v], DP [J] + 1) ;}// the statistical result outputs int sum = 0, max_val = 0; for (INT I = 1; I <= 100; I ++) {sum + = DP [I]; max_val = max (max_val, DP [I]);} printf ("%. 2f % d \ n ", sum/100.0, max_val );}}

Poj 1252 euro efficiency (BFS or 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.