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.
Input
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.
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”.
Sample 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
Sample Output
Case 1: Yes
Case 2: No
//普通的方法:
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <math.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
int a,b;
double c;
char s[31];
} p[1001], e[1001];
int n,k;
int Bellman_Ford(int s,double v)//使用Bellman_Ford去判断是否存在一条正权边可以无限松弛
{
int i,j;
double dis[505];//dis[i] i是货币编号 dis是该种货币的价值
memset(dis,0,sizeof(dis));
dis[s]=v;
for(i=1; i<=n-1; i++)
{
for(j=0; j<k; j++)
{
if(dis[e[j].b]<(dis[e[j].a]*e[j].c*1.0))
dis[e[j].b]=dis[e[j].a]*e[j].c*1.0;
}
}
for(j=0; j<k; j++)//判断正权回路
{
if(dis[e[j].b]<(dis[e[j].a]*e[j].c*1.0))
return 1;
}
return 0;
}
int main()
{
int cnt=0,i,j,m,a,b;
double c;
char st1[31],st2[31];
while(scanf("%d",&n)!=EOF&&n)
{
cnt++;
memset(p,0,sizeof(p));
memset(e,0,sizeof(e));
for(i=0; i<n; i++)
{
//getchar();
scanf("%s",p[i].s);
}
scanf("%d",&m);k=0;
for(i=0; i<m; i++)
{
getchar();
scanf("%s %lf %s",st1,&c,st2);
for(j=0; j<n; j++)
{
if(strcmp(st1,p[j].s)==0) a=j;
if(strcmp(st2,p[j].s)==0) b=j;
}
e[k].a=a;
e[k].b=b;
e[k++].c=c;
}
printf("Case %d: ",cnt);
if(Bellman_Ford(0,1.0)) printf("Yes\n");
else printf("No\n");
}
return 0;
}
//用map函数讲 string 映射成 int
#include <stdio.h>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <string>/***/
#include <queue>
#include <map>
#include <math.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
int to,next;
double w;
} edge[1001];
int head[1001],cnt;
void add(int u,int v,double w)
{
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int n,m;
int spfa(int s)
{
queue<int>quee;
int u,i,v;
double dis[101];
int out[101];
int vis[101];
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(out,0,sizeof(out));
quee.push(s);
vis[s]=1;
dis[s]=1;
while(!quee.empty())
{
u=quee.front();
quee.pop();
vis[u]=0;
for(i=head[u]; i!=-1; i=edge[i].next)
{
v=edge[i].to;
if(dis[v]<dis[u]*edge[i].w*1.0)
{
dis[v]=dis[u]*edge[i].w*1.0;
out[v]++;
if(out[v]>n) return 0;//松弛超过n次存在正权回路
if(!vis[v])
{
quee.push(v);
vis[v]=1;
}
}
}
}
return 1;
}
int main()
{
int ct=0,i;
double c;
string st1,st2,st;
while(scanf("%d",&n)!=EOF&&n)
{
ct++;
map<string,int> ys;/***/
for(i=0; i<n; i++)
{
cin >> st; /***/
ys[st]=i;//映射成数字编号
}
scanf("%d",&m);
cnt=0;
memset(head,-1,sizeof(head));
while(m--)
{
cin >> st1 >>c >>st2;
add(ys[st1],ys[st2],c);
}
printf("Case %d: ",ct);
if(spfa(0)) printf("No\n");
else printf("Yes\n");
}
return 0;
}