hdu 1217 Arbitrage (Floyd)
http://acm.hdu.edu.cn/showproblem.php?pid=1217
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.
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.
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
题目大意:
让你判断是否存在经过若干次货币的兑换使得某种货币的
价值大于原来本身的价值,比如所:美元:美元 = 1 : 1;
题意就是让你判断,在当前的货币兑换率的基础上,能不能
使 美元 : 美元 > 1 : 1; 利用Floyd算法即可搞定
价值大于原来本身的价值,比如所:美元:美元 = 1 : 1;
题意就是让你判断,在当前的货币兑换率的基础上,能不能
使 美元 : 美元 > 1 : 1; 利用Floyd算法即可搞定
代码:
1 #include<iostream> 2 #define INF 999999 3 using namespace std; 4 char str[40][100]; 5 double map[40][40]; 6 int findpos(char *s,int n) 7 { 8 int i=1; 9 for(i=1;i<=n;i++) 10 if(strcmp(str[i],s)==0) 11 return i; 12 return -1; 13 } 14 void floyd(int n) 15 { 16 int i,j,k; 17 for(k=1;k<=n;k++) 18 for(i=1;i<=n;i++) 19 for(j=1;j<=n;j++) 20 if(map[i][j]<map[i][k]*map[k][j]) 21 map[i][j]=map[i][k]*map[k][j]; 22 } 23 int main() 24 { 25 int m,n,i,j,k=1; 26 char str1[100],str2[100]; 27 double df; 28 while(~scanf("%d",&n),n) 29 { 30 for(i=1;i<=n;i++) 31 for(j=1;j<=n;j++) 32 map[i][j]=i==j?0:-1; 33 for(i=1;i<=n;i++) 34 scanf("%s",str[i]); 35 scanf("%d",&m); 36 while(m--) 37 { 38 scanf("%s %lf %s",str1,&df,str2); 39 map[findpos(str1,n)][findpos(str2,n)]=df; 40 } 41 floyd(n); 42 if(map[1][1]>1) 43 printf("Case %d: Yes\n",k++); 44 else 45 printf("Case %d: No\n",k++); 46 } 47 return 0; 48 }