## ARBITRAG - Arbitrage

no tags

Arbitrage is the use of discrepancies in currency exchange rates to transform 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 pounds, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollars. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent.

Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.

### Input

The input file will contain one or more test cases. On the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Note that ci and cj may be the same currency. Exchanges which do not appear in the table are impossible.
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.

### Output

For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes", respectively "Case case: No".

### Example

```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

Output:
Case 1: Yes
Case 2: No```

## Code Examples

### #1 Code Example with C++ Programming

```Code - C++ Programming```

``````#include <iostream>
#include <queue>
#include <vector>
#include <map>
using namespace std;
int N,M;
map<string, int> nodes;
double v[300][300];

int main ()
{
string a,b,c;
int i,j;
double k;
int cas=1;
while(1){
nodes.clear();
cin >> N;
if(N==0)
break;
for(i=0;i<N;i++){
cin >> a;
nodes[a]=i;
}
for(i=0;i<N;i++)
for(j=0;j<N;j++){
v[i][j]=0.0;
}
cin >> M;

for(int z=0;z<M;z++){
cin >> a >> k >> b;
i=nodes.find(a)->second;
j=nodes.find(b)->second;
v[i][j]=k;
}
for(int via=0; via<N;via++)
for(int f=0;f<N;f++)
for(int t=0;t<N;t++)
if(v[f][t] < v[f][via]*v[via][t])
v[f][t] = v[f][via]*v[via][t];

for(i=0;i<N;i++)
if(v[i][i]>1.0)
break;
cout << "Case " << cas << ": ";
if(i==N)
cout << "No" << endl;
else
cout << "Yes" << endl;
cas++;
}
return 0;
}``````
Copy The Code &

Input

cmd
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

Output

cmd
Case 1: Yes
Case 2: No

### #2 Code Example with Java Programming

```Code - Java Programming```

``````/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

/**
*
* @author mdshohag
*/
public class Main{

public static void main(String[] args) {
InputStream stream=System.in;
OutputStream outputstream=System.out;
int test=0;
while (true) {
int n=in.NextInt();
if(n==0)break;
String s;
Map<String,Integer>mymap=new HashMap<String, Integer>();
mymap.clear();
for(int i=0;i<n;i++)
{
s=in.Next();
mymap.put(s, i);
}
int m;
String source,distantion;
double cost;
double [][]data=new double[50][50];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
data[i][j]=0.0;
m=in.NextInt();
for(int i=0;i<m;i++)
{
source=in.Next();
cost=in.NextDouble();
distantion=in.Next();
int x,y;
x=mymap.get(source);
y=mymap.get(distantion);
data[x][y]=cost;
}
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(data[i][j]<data[i][k]*data[k][j])
data[i][j]=data[i][k]*data[k][j];
boolean flag=false;
for(int i=0;i<n;i++)
if(data[i][i]>1.0)
{
flag=true;
break;
}
if(flag)
{
System.out.printf("Case %d: Yes\n", ++test);
}
else
{
System.out.printf("Case %d: No\n", ++test);
}
}

}
}
{
public StringTokenizer tokenizer;
{
tokenizer=null;
}
public String Next()
{
while(tokenizer==null || !tokenizer.hasMoreTokens())
{
try {
} catch (IOException e) {
throw  new RuntimeException();
}
}
}
public int NextInt()
{
return Integer.parseInt(Next());
}
public double NextDouble()
{
return Double.parseDouble(Next());
}
public float NextFloat()
{
return Float.parseFloat(Next());
}
}``````
Copy The Code &

Input

cmd
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

Output

cmd
Case 1: Yes
Case 2: No