Arbitrage
Time limit1sMemory limit128 MB
Given currencies and exchange rates, decide whether some cycle of conversions turns one unit of a currency into more than one unit.
- Level
Medium6 of 10
- Topics
- Shortest path, Graph, Dynamic programming, Math
- Solved
- No attempts yet
Problem
Arbitrage is the use of discrepancies in currency exchange rates to turn one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies in turn, a clever trader can start with 1 US dollar and end up with US dollars, making a 5 percent profit.
Write a program that reads a list of currency exchange rates and determines whether such arbitrage is possible.
Input
The input contains one or more test cases. The first line of each test case has an integer (), the number of different currencies. The next lines each give the name of one currency; a name contains no spaces. The next line contains an integer , the length of the exchange-rate table that follows. Each of the next lines contains the name of a source currency, a real number giving the exchange rate from to , and the name of the destination currency. Any exchange that does not appear in the table is impossible.
Test cases are separated by a blank line. The input ends with a line containing a single for .
Output
For each test case, print one line. If arbitrage is possible, print it in the format "Case : Yes"; otherwise print "Case : No". Here is the test case number, starting from 1.