POJ2240 Arbitrage

                                                                                            Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10997   Accepted: 4622

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

Source

 
 
思路:题目首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率。如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 BritishPound。问在这N种货币中是否有货币可以经过若干次兑换后,兑换成原来的货币可以使货币量增加。
        本题其实是FLOYD的变形。将变换率作为构成图的路径的权重。这儿构成的图是一个有向图。最后将松弛操作变换为:if(dis[i][j]<dis[i][k]*dis[k][j])。
 
  1 #include <cstdlib>
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <cmath>
  6 #include <string>
  7 #include <map>
  8 
  9 #define MAXINT 99999999
 10 
 11 using namespace std;
 12 
 13 
 14 map<string,int>maps;
 15 
 16 int length=1;
 17 
 18 
 19 int insertv(string v)
 20 {
 21     if(maps.find(v)==maps.end())
 22     {maps[v]=length++;}
 23     return 0;
 24 }
 25 
 26 
 27 
 28 
 29 
 30 int findv(string v)
 31 {
 32     return maps[v];
 33 }
 34 
 35 
 36 
 37 
 38 double data[34][34];
 39 
 40 
 41 int ncount=0;
 42 
 43 
 44 int main(int argc, char *argv[])
 45 {
 46   int n,m;
 47   int i,j,k;
 48   
 49   while(scanf("%d",&n)!=EOF)
 50   {
 51                             ncount++;
 52                             
 53                             
 54                             if(n==0)
 55                             break;
 56                             getchar();
 57                             length=1;
 58                             
 59                             maps.clear();
 60                             
 61                             
 62                             
 63                             for(i=0;i<n;i++)
 64                             {char str[1000];
 65                              scanf("%s",str);
 66                              getchar();
 67                              insertv(str);
 68                             }
 69                             
 70                             
 71                             scanf("%d",&m);
 72                            
 73                             getchar();
 74                             
 75                             for(i=1;i<length;i++)
 76                             for(j=1;j<length;j++)
 77                             data[i][j]=1;
 78                             
 79                             for(i=1;i<length;i++)
 80                             data[i][i]=1;
 81                             
 82                             
 83                             
 84                             
 85                             
 86                             for(i=0;i<m;i++)
 87                             {
 88                                             char a[1000],b[1000];
 89                                             double rate;
 90                                             
 91                                             scanf("%s%lf%s",a,&rate,b);
 92                                             
 93                                             //cout<<a<<' '<<rate<<' '<<b<<endl;
 94                                             
 95                                             int v1=findv(a);
 96                                             int v2=findv(b);
 97                                             
 98                                             data[v1][v2]=rate;
 99                                             
100                                             
101                                             
102                                             
103                                             getchar();
104                             }
105                             
106                             
107                             
108                             for(k=1;k<=n;k++)
109                             for(i=1;i<=n;i++)
110                             for(j=1;j<=n;j++)
111                             {
112                                              if(data[i][j]<data[i][k]*data[k][j])
113                                              data[i][j]=data[i][k]*data[k][j];
114                             }
115                             
116                             
117                             
118                             double maxcurr=0;
119                             
120                             
121                             for(i=1;i<=n;i++)
122                             {
123                                              if(maxcurr<data[i][i])
124                                              maxcurr=data[i][i];
125                             }
126                             
127                             printf("Case %d: ",ncount);
128                             if(maxcurr>1.0)
129                             printf("Yes\n");
130                             else
131                             printf("No\n");
132   }
133                             
134                                              
135                             
136                             
137                             
138                             
139                             
140                             
141     
142     
143     
144     
145     //system("PAUSE");
146     return EXIT_SUCCESS;
147 }

 

posted @ 2012-08-18 11:40  cseriscser  阅读(1066)  评论(0编辑  收藏  举报