HDOJ---1217 Arbitrage[弗洛伊德算法]

Arbitrage

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2369    Accepted Submission(s): 1082


Problem 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 file 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
 

 

Recommend
Eddy
 
 
 
 
 
模版,理解三个点的意义
code:
  1 #include <iostream>   

  2 #include <iomanip>   

  3 #include <fstream>   

  4 #include <sstream>   

  5 #include <algorithm>   

  6 #include <string>   

  7 #include <set>   

  8 #include <utility>   

  9 #include <queue>   

 10 #include <stack>   

 11 #include <list>   

 12 #include <vector>   

 13 #include <cstdio>   

 14 #include <cstdlib>   

 15 #include <cstring>   

 16 #include <cmath>   

 17 #include <ctime>   

 18 #include <ctype.h> 

 19 using namespace std;

 20 

 21 #define MAXN 35

 22 

 23 char str[MAXN][20];

 24 double map[MAXN][MAXN];

 25 int n,m;

 26 

 27 void init()

 28 {

 29     int i,j;

 30     for(i=0;i<n;i++)

 31         for(j=i;j<n;j++)

 32             map[i][j]=map[j][i]=0;

 33 }

 34 

 35 int fun(char a[])

 36 {

 37     int i;

 38     for(i=0;i<n;i++)

 39         if(strcmp(str[i],a)==0)

 40             return i;

 41 }

 42 

 43 int main()

 44 {

 45     int i,j,k;

 46     char a[20],b[20];

 47     double temp;

 48     int cnt=1;

 49     while(~scanf("%d",&n),n)

 50     {

 51         init();

 52         for(i=0;i<n;i++)

 53             scanf("%s",str[i]);

 54         scanf("%d",&m);

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

 56         {

 57             scanf("%s %lf %s",a,&temp,b);

 58             map[fun(a)][fun(b)]=temp;

 59         }

 60         bool flag=false;

 61         for(k=0;k<n;k++)                                  //中间点

 62         {

 63             for(i=0;i<n;i++)                              //起点

 64             {

 65                 for(j=0;j<n;j++)                           //终点

 66                 {

 67                     if(map[i][j]<map[i][k]*map[k][j])

 68                     {

 69                         map[i][j]=map[i][k]*map[k][j];

 70                         if(map[k][k]>1)

 71                         {

 72                             flag=true;

 73                             break;

 74                         }

 75                     }

 76                 }

 77                 if(flag)

 78                     break;

 79             }

 80             if(flag)

 81                 break;

 82         }

 83         if(flag)

 84             printf("Case %d: Yes\n",cnt++);

 85         else

 86             printf("Case %d: No\n",cnt++);

 87     }

 88     return 0;

 89 }

 90 /*

 91 4

 92 a

 93 b

 94 c

 95 d

 96 9

 97 a 10 b

 98 b 10 c

 99 c 0.2 b

100 a 0.3 c

101 c 2 a

102 c 100 d

103 d 0.02 c

104 a 100 d

105 d 0.001 a

106 yes

107 */

 

你可能感兴趣的:(bit)