#P1931. Arbitrage Detection
Arbitrage Detection
Arbitrage Detection
This problem involves detecting an arbitrage opportunity in a currency exchange market. Arbitrage is the process of taking advantage of a price difference between two or more markets: it is the ability to buy a currency at one rate and simultaneously sell it at a higher rate in another market to secure a profit.
For example, consider the following exchange rates: \[ 1\,USD \rightarrow 0.5\,GBP,\; 1\,GBP \rightarrow 10.0\,FRF,\; 1\,FRF \rightarrow 0.21\,USD \] Starting with 1 USD, you can exchange it to 0.5 GBP, then to 5.0 FRF, and finally back to 1.05 USD, thus achieving a profit of 5%.
Your task is to write a program that reads in a list of exchange rates and determines whether there exists an arbitrage cycle (a sequence of exchanges that leads to more money than you started with).
inputFormat
The input consists of multiple lines:
- The first line contains an integer n which denotes the number of exchange rates.
- The following n lines each contain an exchange rate in the format:
source_currency target_currency rate
, wheresource_currency
andtarget_currency
are strings without spaces, andrate
is a positive floating-point number.
You can assume that all currency codes are case-sensitive.
outputFormat
Output a single line: Yes
if there is an arbitrage opportunity, and No
otherwise.
sample
3
USD GBP 0.5
GBP FRF 10.0
FRF USD 0.21
Yes
</p>