This page is still under construction.

Parts of this page are still being built. What you see may change.

Arbitrage

Time limit1sMemory limit128 MB

Summary
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 0.5×10.0×0.21=1.050.5 \times 10.0 \times 0.21 = 1.05 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 nn (1≤n≤301 \le n \le 30), the number of different currencies. The next nn lines each give the name of one currency; a name contains no spaces. The next line contains an integer mm, the length of the exchange-rate table that follows. Each of the next mm lines contains the name cic_i of a source currency, a real number rijr_{ij} giving the exchange rate from cic_i to cjc_j, and the name cjc_j 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 00 for nn.

Output

For each test case, print one line. If arbitrage is possible, print it in the format "Case cc: Yes"; otherwise print "Case cc: No". Here cc is the test case number, starting from 1.

Examples3

  1. Example 1

    Input
    3
    USDollar
    BritishPound
    FrenchFranc
    3
    USDollar 0.5 BritishPound
    BritishPound 10.0 FrenchFranc
    FrenchFranc 0.21 USDollar
    
    3
    USDollar
    BritishPound
    FrenchFranc
    6
    USDollar 0.5 BritishPound
    USDollar 4.9 FrenchFranc
    BritishPound 10.0 FrenchFranc
    BritishPound 1.99 USDollar
    FrenchFranc 0.09 BritishPound
    FrenchFranc 0.19 USDollar
    
    0
    
    Expected output
    Case 1: Yes
    Case 2: No
    
  2. Example 2

    Input
    2
    Alpha
    Beta
    2
    Alpha 2.0 Beta
    Beta 0.6 Alpha
    
    0
    
    Expected output
    Case 1: Yes
    
  3. Example 3

    Input
    2
    Dollar
    Euro
    2
    Dollar 2.0 Euro
    Euro 0.4 Dollar
    
    0
    
    Expected output
    Case 1: No