#P1931. 套利

套利

Description

Arbitrage is the practice of profiting from differences in exchange rates. For example, 11 dollar can be exchanged for 0.50.5 pounds, 11 pound can be exchanged for 1010 francs, and 11 franc can be exchanged for 0.210.21 dollars. Then, a clever trader can start from 11 dollar and get 0.5×10.0×0.21=1.050.5 \times 10.0 \times 0.21 = 1.05 dollars, earning a 5%5\% profit.

Your task is to write a program that reads a list of exchange rates from the input file and then determines whether arbitrage is possible or not.

Input Format

The input file contains multiple datasets. The first line of each dataset is an integer nn (1n301 \le n \le 30), representing the number of currencies.

The next nn lines each contain a string, which is the name of a currency (there will be no spaces in the names). The next line is an integer mm, specifying the length of the exchange rate table. The following mm lines are each in the form ci,ri,j,cjc_i, r_{i,j}, c_j. Here, cic_i is the name of the source currency, the real number ri,jr_{i,j} is the exchange rate from cic_i to cjc_j, and cjc_j is the name of the target currency. Conversions not listed are impossible.

There is a blank line between datasets. When n=0n = 0, the input ends.

Output Format

For the ii-th dataset, output Case i: followed by Yes if arbitrage is possible, otherwise output No.

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

Case 1: Yes
Case 2: No

Hint

Translated by ChatGPT 5