#P1875. Magical Potion Crafting
Magical Potion Crafting
Magical Potion Crafting
You are given a target potion that you wish to obtain. In the magical world, any potion is available for purchase at a magic store at a given price. In addition, a magic book contains several recipes. Each recipe specifies that mixing one unit of two specific potions produces one unit of another potion (i.e. 1 unit of A plus 1 unit of B yields 1 unit of C). Note that the arithmetic of potions is magical so that indeed 1+1 can equal 1!
Starting with no potions in hand, you may either buy potions directly from the store or use any recipe detailed in the magic book to craft them. Given the target potion, the store prices for all available potions, and all the available recipes, determine:
- The minimum cost needed to obtain the target potion.
- The total number of distinct ways to achieve this minimum cost. Two methods are considered different if there is at least one step (choice of recipe or purchase) that differs.
Note: It is guaranteed that every potion is available in the store, and each recipe is of the form \(1 \text{ potion}_1 + 1 \text{ potion}_2 = 1 \text{ potion}_3\).
inputFormat
The input is given in the following format:
TargetPotion n PotionName1 Price1 PotionName2 Price2 ... PotionN PriceN m Ingredient1 Ingredient2 Product ... (total m recipes)
Where:
TargetPotion
is the name of the potion you want to obtain.n
is the number of potions. The followingn
lines list each potion's name and its store price.m
is the number of available recipes. Each subsequent line describes a recipe in the format: two ingredient potion names followed by the resulting product potion name.
outputFormat
Output two space‐separated integers on a single line:
- The minimum cost to obtain the target potion.
- The number of distinct ways to achieve this minimum cost.
sample
C
3
A 3
B 4
C 10
1
A B C
7 1