<div class="ptt" lang="en-US" style="text-align: center; font-size: 18pt; font-weight: bold; color: blue;">Arbitrage</div><div class="plm" style="text-align: center;font-size:14px;"><table align="center"><tbody><tr><td><strong>Time Limit:</strong> 1000MS</td><td width="10px"> </td><td><strong>Memory Limit:</strong> 65536K</td></tr><tr><td><strong>Total Submissions:</strong> 20515</td><td width="10px"> </td><td><strong>Accepted:</strong> 8738</td></tr></tbody></table></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Description</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif;font-size:14px;">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 pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. 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.
</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Input</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif;font-size:14px;">The input will contain one or more test cases. Om 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. 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.</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Output</p><div class="ptx" lang="en-US" style="font-family: 'Times New Roman', Times, serif;font-size:14px;">For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Sample Input</p><pre class="sio" style="font-family: 'Courier New', Courier, monospace;font-size:14px;">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
Sample Output
Case 1: Yes Case 2: No
Source
#include<stdio.h>
#include<string.h>
double map[100][100];
char s[100][100];
char s1[100];
int t;
int look()
{
scanf("%s",s1);
for(int i=0;i<t;i++)
{
if(strcmp(s1,s[i])==0)
return i;
}
}
void flyd()
{
int i,j,k;
for(k=0;k<t;k++)
{
for(i=0;i<t;i++)
{
for(j=0;j<t;j++)
if(map[i][j]<map[i][k]*map[k][j])
map[i][j] = map[i][k]*map[k][j];
}
}
}
int main()
{
int i,j,k,n;
double v;
int cas=0;
while(~scanf("%d",&t),t)
{
memset(map,0,sizeof(map));
memset(s,0,sizeof(s));
for(i=0;i<t;i++)
{
scanf("%s",s[i]);
}
// for(i=0;i<t;i++)
//printf("%s\n",s[i]);
for(i=0;i<t;i++)
for(j=0;j<t;j++)
map[i][j]=1.0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
int a=look();
scanf("%lf",&v);
int b=look();
//printf("%d %d\n",a,b);
map[a][b]=v;
// printf("%.3lf\n",map[a][b]);
}
int flag=0;
flyd();
// for(i=0;i<t;i++)
// for(j=0;j<t;j++)
// printf("%d %d %.3lf\n",i,j,map[i][j]);
for(i=0;i<t;i++)
if(map[i][i]>1.0)
{
//printf();
flag=1;
break;
}
cas++;
if(flag) printf("Case %d: Yes\n",cas);
else printf("Case %d: No\n",cas);
}
}