Arbitrage

 

Description

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


最短路,一直很纠结,一直搞不懂,第一次自己A最短路,看着模板敲啊
 1 #include<iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 #include<map>

 5 using namespace std;

 6 struct node

 7 {

 8     int a,b;

 9     double v;

10 } g[3500];//储存钱之间的汇率

11 int main()

12 {

13     int m,n,i,j,num=0;

14     double v,dis[3500];

15     char money[1000],moneyt[1000];//谁让钱是字符串呢

16     while(cin>>n&&n)

17     {

18         num++,n++;

19         map<string,int>mapp;//为了方便把钱编号,用数组可以模拟,太麻烦

20         map<string,int>::iterator iter;//声明迭代器

21         for(i=1; i<n; i++)

22         {

23             cin>>money;

24             mapp.insert(pair<string,int>(money,i));//插入钱和序号,相当于编号

25         }

26         cin>>m;

27         for(i=0; i<m; i++)

28         {

29             scanf("%s %lf %s",money,&v,moneyt);//输入兑换比例

30             iter=mapp.find(money);//查找对应序号

31             g[i].a=iter->second;

32             g[i].v=v;

33             iter=mapp.find(moneyt);//查找对应序号

34             g[i].b=iter->second;

35         }

36         memset(dis,0,sizeof(dis));//标记数组置零

37         dis[1]=1;

38         for(i=2; i<n; i++)//n-1次松弛

39             for(j=0; j<m; j++)

40                 if(dis[g[j].b]<dis[g[j].a]*g[j].v)

41                     dis[g[j].b]=dis[g[j].a]*g[j].v;

42         int flag=0;

43         for(j=0; j<m; j++)//还可以继续变大,就说明可以赚钱啊

44             if(dis[g[j].b]<dis[g[j].a]*g[j].v)

45                 flag=1;

46         printf("Case %d: ",num);

47         if(flag)

48             printf("Yes\n");

49         else

50             printf("No\n");

51         }

52     return 0;

53 }
View Code

 

你可能感兴趣的:(bit)