# POJ - 2240 Arbitrage （最短路 Floyd算法 && Bellman-Ford算法）

## Arbitrage

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

Floyd算法：

因为最多30种货币，所以用Flayd算法也可以，Floyd算法的思想也比较好理解，主要就是图的构造，（看代码注释）需要注意的是，题目要求最大汇率关系，所以更新数据时是求大于本身的，并且权值为相乘关系，具体看代码注释。

AC代码：

``````#include<stdio.h>
#include<string.h>

char huo[36][36];
char aa[36], bb[36];
double mp[36][36], x;

int main()
{
int n, g = 1;
while( scanf("%d",&n) && n )
{
for( int i = 1; i <= n; i++ )
scanf("%s",huo[i]);     //存入货币种类
int m;                      //初始化图，因为没有汇率关系的
memset(mp,0,sizeof(mp));    //货币之间不能兑换，赋值为0
for( int i = 1; i <= n; i++ )
mp[i][i] = 1;           //同种货币之间汇率为1
scanf("%d",&m);
for( int i = 1; i <= m; i++ )
{
int x1, x2;
scanf("%s%lf%s",aa,&x,bb);
for( int j = 1; j <= n; j++ )
{
if( strcmp(huo[j],aa) == 0 ) //查找是第几种货币，转化为点
{
x1 = j;
break;
}
}
for( int j = 1; j <= n; j++ )
{
if( strcmp(huo[j],bb) == 0 ) //查找是第几种货币，转化为点
{
x2 = j;
break;
}
}
mp[x1][x2] = x;   //赋权值
}
for( int k = 1; k <= n; k++ )
{
for( int i = 1; i <= n; i++ )
{
for( int j = 1; j <= n; j++ )
{
if( mp[i][j] < mp[i][k] * mp[k][j] ) //因为找最大汇率关系，所以为“<”和“*”
mp[i][j] = mp[i][k] * mp[k][j];
}
}
}
int flag = 0;
for( int i = 1; i <= n; i++ )
{
if( mp[i][i] > 1 ) //发现有超过本身的值，说明有套汇现象
{
flag = 1;
break;
}
}
printf("Case %d: ",g++);
if( flag )
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
``````

Bellman-Ford算法：

AC代码：

``````#include<stdio.h>
#include<string.h>
#define maxn 50 //顶点数最大值
#define maxm 1000 //边数最大值
#define max(a,b) ( (a)>(b) ? (a):(b) )

struct exchange //汇率关系
{
int ci, cj;
double cij;
}ex[maxm]; //汇率关系数组

int i, j, k; //循环变量
int n, m;    //货币种类、汇率数目
int flag, kase = 0;
char name[maxn][20], a[20], b[20];
double x, maxdist[maxn]; //汇率、源点i到其他每个顶点（包括它本身）的最长路径长度

{
scanf("%d",&n);
if( n == 0 )
return 0;
for( i = 0; i < n; i++ ) //读入n个货币名称
scanf("%s",name[i]);
scanf("%d",&m);
for( int i = 0; i < m; i++ ) //读入汇率
{
scanf("%s%lf%s",a,&x,b);
for( j = 0; strcmp(a,name[j]); j++ )
;
for( k = 0; strcmp(b,name[k]); k++ )
;
ex[i].ci = j; ex[i].cij = x; ex[i].cj = k;
}
return 1;
}

//Bellman-Ford算法;以顶点v0为源点，求它到每个顶点（包括它本身）的最大距离
void bellman( int v0 )
{
flag = 0;
memset(maxdist,0,sizeof(maxdist));
maxdist[v0] = 1;
for( k = 0; k < n; k++ ) //从maxdist(0)递推到maxdist(1),...,maxdist(n)
{
for( i = 0; i < m; i++ ) //判断每条边，加入它是否能使得最大距离增加
{
if( maxdist[ex[i].ci] * ex[i].cij > maxdist[ex[i].cj] )
maxdist[ex[i].cj] = maxdist[ex[i].ci] * ex[i].cij;
}
}
if( maxdist[v0] > 1.0 )
flag = 1;
}

int main()
{
{
for( i = 0; i < n; i++ )
{
bellman(i);  //从第i个顶点出发求最长路径
if( flag ) break;
}
if(flag) printf("Case %d: Yes\n",++kase);
else printf("Case %d: No\n",++kase);
}
return 0;
}
``````

原文作者：Bellman - ford算法
原文地址: https://blog.csdn.net/hao_994/article/details/76168537
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。