HDU 1217 Arbitrage(最短路径,Floyd算法)
Posted cake-lover-77
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1217 Arbitrage(最短路径,Floyd算法)相关的知识,希望对你有一定的参考价值。
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
最短路径,弗洛伊德算法,dp,用到了map函数
1 #include<stdio.h> 2 #include<string> 3 #include<iostream> 4 #include<map> 5 using namespace std; 6 const int INF=0.00001; 7 int n,m,t; 8 double dp[50][50],c; 9 string s[50],x1,x2; 10 map<string,int>p; 11 12 int main(){ 13 t=1; 14 while(scanf("%d",&n)!=EOF&&n){ 15 for(int i=1;i<=n;i++) 16 for(int j=1;j<=n;j++) 17 dp[i][j]=INF; //初始化 18 for(int i=1;i<=n;i++){ 19 cin>>s[i]; 20 p[s[i]]=i; 21 } 22 /* map<string,int>::iterator iter; //map函数的用法 23 for(iter=p.begin();iter!=p.end();iter++) 24 cout<<iter->first<<" "<<iter->second<<endl;*/ 25 cin>>m; 26 for(int i=1;i<=m;i++){ 27 cin>>x1>>c>>x2; 28 dp[p[x1]][p[x2]]=c; 29 } 30 for(int i=1;i<=n;i++)//floyd算法 31 for(int j=1;j<=n;j++) 32 for(int k=1;k<=n;k++) 33 if(dp[j][i]*dp[i][k]>dp[j][k]) 34 dp[j][k]=dp[j][i]*dp[i][k]; 35 printf("Case %d: ",t++); 36 if(dp[1][1]>1)//因为是自己到自己的转换,所以dp[1][1],如果自己到终点的转换,dp[1][n] 37 printf("Yes "); 38 else 39 printf("No "); 40 } 41 }
以上是关于HDU 1217 Arbitrage(最短路径,Floyd算法)的主要内容,如果未能解决你的问题,请参考以下文章