Shop shopping (IOI ' 95)

Source: Internet
Author: User


First, the problem description



There is a price for each item in a store. For example, the price of a flower is 2 ICU (ICU is the unit of the Currency of the informatics contest); The price of a vase is 5 ICU. In order to attract more customers, the store offers special rates.






A special offer is a grouping of one or several commodities. and reduced sales. For example: The price of 3 flowers is not 6 but 5 ICU, 2 vases plus 1 flowers are ICU not ICU.






Compile a procedure to calculate the cost of goods purchased by a customer. Make full use of the discounted price to minimize customer payments. Please note that you cannot change the type and quantity of the goods purchased by the customer, even if the addition of certain items will reduce the total amount of the payment and not allow you to make any changes. Suppose the prices of various commodities are discounted as described above, and a customer buys items: 3 flowers and 2 vases. Then the customer payables are in ICU because:






1 Flower plus 2 vases: Special Price: ICU






2 Flower Normal Price: 4 ICU






Input data






The input data is represented by two files. The first file of input. TXT describes the items purchased by the customer (placed in the shopping basket); The second document describes the goods and prices offered by the store (the file is named offer. TXT). Only integers are used in all two files.






The first file of input. The TXT format is: The first line is a number B (0≤b≤5), which indicates the number of items purchased. Below a total of B lines, each line contains 3 number c,k,p. C represents the code of the commodity (each commodity has a unique encoding), 1≤c≤999. K represents the total number of purchases of this commodity, 1≤k≤5. P is the normal unit price of the commodity (prices per item), 1≤p≤999. Please note that a maximum of 5*5=25 items can be placed in the shopping basket.






Second file offer. The TXT format is: The first line is a number s (0≤s≤99), representing a total of S-type offers. Below a total of S-lines, each line describes the kind of goods in a combination of preferential products. Next there are several pairs of numbers (c,k), where C is the product code, 1≤C≤9 99. K represents the quantity of the product in this combination, 1≤k≤5. The bank's last digit P (1≤p≤9999) represents the preferential price for this portfolio. Of course, the preferential price is lower than the sum of the normal price of the product in this combination.












Output data






The output file outputs. TXT write a number (one line) that represents the minimum amount of goods that a customer buys (the input document specifies the goods purchased).






Second, analysis



At first glance at this topic, my feeling is familiar, and we do the knapsack problem is similar. Just the knapsack problem is a given capacity, to find the most value of something. And this topic is given the things put, the minimum cost (corresponding to the knapsack problem is the smallest capacity). Happens to be a "inverse problem" of the most value. Knapsack problem is a classic dynamic programming problem, then this question.



Since the dynamic programming is to meet the no-effect and optimization principle, we analyze whether the problem satisfies the above two points. First, the method of state representation, no more than 5 kinds of goods, and the quantity of each purchase is not more than 5, then use a five-tuple to indicate the minimum cost of AI for the first commodity. :



F (A1 , A2 , A3 , A4 , A5 ) (1 )



Consider the origin of this state, of course, we do not have preferential products can buy, obviously this is not the best. So if we can use the product combination of article I, the State handy in order to:



F (A1-si1 , A2-si2 , A3-si3 , A4-si4 , A5-si5 ) (2 )





In this case, the cost of state 1 is the cost of state 2 plus the cost of SI, and the cost of state 2 must be the lowest (obviously, with contradiction), at the same time, we also no matter how the status of 2 (because each of the use of the preferential product combination is unlimited), so the subject is not only to meet the non-validity, At the same time there are a large number of overlapping sub-problem generation, dynamic planning to solve the problem is best.



Through the analysis of the problem, we know the basic method of representation and transfer of state, we can easily get a state transfer equation:



f [A, B, C, D, e] = Min {F [A-s1, B-s2, C-s3, D-S4, E-S5] + salecost [S]}



The initial conditions are:



F [A, B, C, D, e] = Cost [1]*a+cost [2]*b+cost] [3]*c+cost [4]*d+cost] [5]*e



That is, no preferential purchase fees.



Third, summary



This problem is still relatively simple, after all, overtaken by events, this is seven or eight years ago the topic. Time complexity is also acceptable, for O (65*99) ≈106, but the effect of constant term is more prominent because of the small number of goods. The spatial complexity of O (65) is not a problem at all. When implemented, because the input data is part of the meaningless, such as the product contains unwanted items, we can be removed from the input, to improve the efficiency of the program. For the data, the total number of goods less than 5, you can not buy those kinds of purchase as 0, to unify the operation.



Iv. Reference Procedures



Program _shop;



Const



name1 = ' input.txt ';



name2 = ' output.txt ';



Name3 = ' offer.txt ';



Type



Saletype = array[1:5] of Byte;



Var



F:array[0:5, 0:5, 0:5, 0:5, 0:5] of Word;



Costs:array[0:5] of Integer;



Code:array[1.999] of Byte;



SALE:ARRAY[1] of Saletype;



Paysale:array[1. "Of Integer;



Check:array[1. [] of Boolean;



St,ed:array[0:5] of Byte;



S,b:integer;









Procedure Init;



var I,cc,kk,pp,j:word;



Begin



Assign (input, name1); Reset (input);



Fillchar (f, sizeof (f), 0);



Fillchar (Code, sizeof (code), 0);



Fillchar (sale, sizeof (sale), 0);



Fillchar (costs, sizeof (costs), 0);



Fillchar (St, sizeof (ST), 0);



Fillchar (Ed, sizeof (ed), 0);



Fillchar (check, sizeof (check), true); {Initialize}



READLN (b);



For I: = 1 to B do begin



READLN (CC, KK, pp);



CODE[CC]: = i;



Ed[i]: = KK; {Ed[i] Indicates the number of items purchased for the first commodity}



Costs[i]: = PP;



End



Close (input);



Assign (input, name3); Reset (input);



READLN (s);



For I: = 1 to S do begin



Read (CC);



for J: = 1 to CC do BEGIN



Read (KK, pp);



If CODE[KK] = 0 THEN BEGIN Check[i]: = false; Break End



{Obviously not the combination will not be counted}



Sale[i, Code[kk]]: = PP;



End



READLN (CC);



Paysale[i]: = CC;



End



Close (input);



End









Procedure main;



var J,k:integer;



I:array[1:5] of Byte;



Q,t:word;



Can:boolean;



Begin



For i[1]: = St[1] to ed[1] do



For i[2]: = st[2] to ed[2] do



For i[3]: = St[3] to ed[3] do



For i[4]: = St[4] to ed[4] do



For i[5]: = St[5] to ed[5] Do begin {enumerate each state}



Q: = 0;



for J: = 1 to 5 do Inc (Q, COSTS[J] * i[j]); {Initial value without any offers}



for J: = 1 to S do {enumerate each offer portfolio}



If CHECK[J] then BEGIN



can: = true;



For k: = 1 to 5 do



If I[K] < Sale[j, K] then can: = false; {Whether the current combination is applicable}



If can then BEGIN



T: = Paysale[j] +





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.