Lemonade Trade

Picture by David Wargert via Flickr.

The lunch break just started! Your mother gave you one litre of pink lemonade. You do not like pink lemonade and want blue lemonade instead. Fortunately for you the other children in class are willing to trade with you.

Each of them is willing to offer you any quantity of the virtually infinite amount of lemonade they got from their mother, in exchange for their favourite lemonade, according to some exchange rate. The other children are sitting in a long row in the class room and you will walk along the row, passing each child only once. You are not allowed to walk back! Of course, you want to maximise the amount of blue lemonade you end up with. In case you can obtain more than $10$ litres of blue lemonade, this is more than you will need, and you will throw away any excess (and keep the $10$ litres).

Fortunately, you know in advance what everybody is offering for trade. Your task is to write a program to find the maximum amount of blue lemonade that you can obtain.


The input consists of the following:

  • One line containing a single integer $0\leq N \leq 10^5$, the number of children in the class room, excluding yourself;

  • $N$ lines, each containing two strings $O, W$ and a floating point number $0.5 < R < 2$, the name of the lemonade that is offered, the name of the lemonade that is wanted, and the exchange rate: for every litre of lemonade $W$ that you trade you get $R$ litres of lemonade $O$ in return. Exchange rates are given with at most $7$ digits after the decimal point.

All strings are guaranteed to have at most $10$ alphanumeric characters.


Output a single line containing a single floating point number $M$, the maximum amount (in litres) of blue lemonade that you can obtain. In case you could obtain more than $10$ litres, $M$ is considered to be $10$. You are required to output $M$ with absolute precision $10^{-6}$.

Sample Input 1 Sample Output 1
blue pink 1.0
red pink 1.5
blue red 1.0
Sample Input 2 Sample Output 2
blue red 1.0
red pink 1.5
Sample Input 3 Sample Output 3
orange pink 1.9
yellow orange 1.9
green yellow 1.9
blue green 1.9
Sample Input 4 Sample Output 4
red pink 1.9
orange red 1.9
yellow orange 1.9
green yellow 1.9
indigo green 0.6
violet indigo 0.6
purple violet 0.6
blue purple 0.6
CPU Time limit 3 seconds
Memory limit 1024 MB
Difficulty 5.8hard
Statistics Show
Languages English, 日本語
License Creative Commons License (cc by-sa)

Please log in to submit a solution to this problem

Log in