![]() |
Shopping OffersDynamic Programming |
= 2,
= 5,
+
+
= 5,
+
+
= 10,
+
+
+
+
= ?
In a shop each kind of product has a price. For example, the price of a flower is 2 ICU (Informatics Currency Units) and the price of a vase is 5 ICU. In order to attract more customers, the shop introduces some special offers.
A special offer consists of one or more product items for a reduced price. Examples: three flowers for 5 ICU instead of 6, or two vases together with one flower for 10 ICU instead of 12.
Write a program that calculates the price a customer has to pay for certain items, making optimal use of the special offers. That is, the price should be as low as possible. You are not allowed to add items, even if that would lower the price.
For the prices and offers given above, the (lowest) price for three flowers and two vases is 14 ICU: two vases and one flower for the reduced price of 10 ICU and two flowers for the regular price of 4 ICU.
The first line of INPUT.TXT contains the number b of different kinds of products in the basket (0<=b<=5). Each of the next b lines contains three values c, k, and p. The value c is the (unique) product code (1<=c<=999). The value k indicates how many items of this product are in the basket (1<=k<=5). The value p is the regular price per item (1<=p<=999). Notice that all together at most 5*5=25 items can be in the basket.
The first line of OFFER.TXT
contains the number s of special offers (0<=s<=99).
Each of the next s lines describes one offer by giving
its structure and its reduced price.
The first number n on such a line is the number of
different kinds of products that are part of the offer (1<=n<=5).
The next n pairs of numbers (c,k) indicate that
k items (1<=k<=5) with product code c (1<=c<=999)
are involved in the offer.
The last number p on the line stands for the reduced price
(1<=p<=9999).
The reduced price of an offer is less than the sum of the regular prices.
Suppose that we have an offering of (a,b,c,d,e) for the price of
x. And suppose that we can buy items (f,g,h,i,j)
for the price of y. It is obvious that we can buy items
(f+a,g+b,h+c,i+d,j+e) for the price of x+y. If
this new price is lower than the price recorded in the array
Best, we initialize the array with this new price.
Output Data
Write to the output file OUTPUT.TXT one line with the lowest possible
price to be paid for the purchases in the input file.
Example Input and Output
Figure 1 gives input and output files for the example above.
The product code of a flower is 7 and that of a vase is 8.
_____________ ________________ ______________
| INPUT.TXT | | OFFER.TXT | | OUTPUT.TXT |
|___________| |______________| |____________|
| 2 | | 2 | | 14 |
| 7 3 2 | | 1 7 3 5 | |____________|
| 8 2 5 | | 2 7 1 8 2 10 |
|___________| |______________|
Figure 1: Example input and output
Solution (Yogy Namara)
We will keep a record of the best price we can get for each
combination of items. We will use a five dimentional array
Best to do that. From now on, we will write the structure
of a set of items as (q1,q2,q3,q4,q5). These values vary
from 0..5. The position in the array is important, so
(1,0,0,0,0) is not the same as (0,1,0,0,0)
yogy-n@sby.mega.net.id