POJ 2240 Arbitrage
Posted 风中的簌雨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2240 Arbitrage相关的知识,希望对你有一定的参考价值。
Arbitrage
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
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 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.
<br>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
题目大意:可以简单描述为知道从i到j的汇率,问能不能赚钱(就是当i到i初始是t时,从i到i的最长路径能不能大于t,如果有这样的情况就是能赚钱)。
解题思路:该题有多种解法,可以用Bellman-Frod算法判断是否存在是否存在正环,也可以用Floyd算法找出最大环
Bellman-Frod算法:
1 #include <stdio.h> 2 #include <string.h> 3 int n,m,ans; 4 char str[40][50]; 5 double dis[40]; 6 struct f 7 { 8 int x,y; 9 double z; 10 }rate[1000]; 11 int num(char s[]) //输出对应货币的编号 12 { 13 int i; 14 for (i = 1; i <= n; i ++) 15 if (strcmp(str[i],s)==0) 16 break; 17 return i; 18 } 19 void add(int a,int b,double c) 20 { 21 rate[ans].x = a; 22 rate[ans].y = b; 23 rate[ans].z = c; 24 ans ++; 25 } 26 bool Bellman_ford() 27 { 28 int i,j; 29 for (i = 1; i <= n; i ++) 30 dis[i] = 0; 31 dis[1] = 100; 32 for (i = 1; i < n; i ++) 33 for (j = 0; j < ans; j ++) 34 if (dis[rate[j].y] < dis[rate[j].x]*rate[j].z) 35 dis[rate[j].y] = dis[rate[j].x]*rate[j].z; 36 for (j = 0; j < ans; j ++) 37 if (dis[rate[j].y] < dis[rate[j].x]*rate[j].z) 38 return true; 39 return false; 40 } 41 int main () 42 { 43 int i,j,f = 1; 44 double c; 45 char ch1[100],ch2[100]; 46 while (~scanf("%d",&n)) 47 { 48 ans = 0; 49 if (n == 0) 50 break; 51 for (i = 1; i <= n; i ++) 52 scanf(" %s",str[i]); 53 54 55 scanf("%d",&m); 56 for (j = 0; j < m; j ++) 57 { 58 scanf("%s %lf %s",ch1,&c,ch2); 59 int a = num(ch1); 60 int b = num(ch2); 61 add(a,b,c); 62 } 63 64 int flag = 0; 65 printf("Case %d: ",f ++); 66 if (Bellman_ford()) 67 printf("Yes\n"); 68 else 69 printf("No\n"); 70 } 71 return 0; 72 }
Floyd算法:
1 //Memory Time 2 //276K 79MS 3 4 #include <iostream> 5 #include<map> 6 #include<string> 7 using namespace std; 8 9 const int inf=10000; //无限大 10 int n; //货币种类 11 int m; //兑换方式 12 13 map<string,int>STL; //建立一个 使字符串与整数有一一对应关系 的容器STL,以便利用邻接矩阵存储数据 14 15 double rate; 16 char str[50],str1[50],str2[50]; 17 double dist[31][31]; 18 19 int i,j,k; 20 21 void floyd(void) 22 { 23 for(k=1;k<=n;k++) 24 for(i=1;i<=n;i++) 25 for(j=1;j<=n;j++) 26 if(dist[i][j] < dist[i][k] * dist[k][j]) //变形的最大路径,变"+"为"*" 27 dist[i][j] = dist[i][k] * dist[k][j]; 28 return; 29 } 30 31 int main(void) 32 { 33 int cases=1; 34 while(cases) 35 { 36 memset(dist,inf,sizeof(dist)); 37 38 39 cin>>n; 40 if(!n)break; 41 42 for(i=1;i<=n;i++) 43 { 44 cin>>str; 45 STL[str]=i; //将输入的货币从1到n依次编号 46 dist[i][i]=1; //到自身的转换率默认为1,但通过floyd可能会被改变 47 //有向图的顶点(一般)存在环 48 } 49 50 cin>>m; 51 for(i=1;i<=m;i++) 52 { 53 cin>>str1>>rate>>str2; 54 dist[STL[str1]][STL[str2]]=rate; //构造图 55 } 56 57 floyd(); 58 59 int flag=false; 60 for(i=1;i<=n;i++) 61 if(dist[i][i]>1) 62 { 63 flag=true; 64 break; 65 } 66 if(flag) 67 cout<<"Case "<<cases++<<": Yes"<<endl; 68 else 69 cout<<"Case "<<cases++<<": No"<<endl; 70 } 71 return 0; 72 }
以上是关于POJ 2240 Arbitrage的主要内容,如果未能解决你的问题,请参考以下文章