#P1931. 套利
套利
Description
Arbitrage is the practice of profiting from differences in exchange rates. For example, dollar can be exchanged for pounds, pound can be exchanged for francs, and franc can be exchanged for dollars. Then, a clever trader can start from dollar and get dollars, earning a 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 (), representing the number of currencies.
The next 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 , specifying the length of the exchange rate table. The following lines are each in the form . Here, is the name of the source currency, the real number is the exchange rate from to , and is the name of the target currency. Conversions not listed are impossible.
There is a blank line between datasets. When , the input ends.
Output Format
For the -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
京公网安备 11011102002149号