poj 2240 Arbitrage bellman ford

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

题目描述:套汇是利用汇率之间的差异,从而将一单位的某种货币,兑换回多于1单位的同种货币。例如,假定1美元兑换0.5英镑,1英镑兑换10.0法郎,1法郎兑换0.21

美元,那么,在兑换货币过程中,一个聪明的商人可以用1美元兑换到0.5 * 10.0 * 0.21 = 1.05美元,这样就有5%的利润。你的任务是编写程序,读入货币之间的汇率列表,判断是否存在套汇。

//bellmam ford

#include <cstdio>
#include <cstring>
#include<iostream>
#define maxn 50 //
#define maxm 1000 //
#define max(a,b) ( (a) > (b) ? (a) : (b) )
using namespace std;

struct exchange //汇率关系
{
    int ci, cj;
    double cij;
} ex[maxm]; //汇率关系数组
int i, j, k; //循环变量
int n, m; //货币种类的数目、汇率的数目
char name[maxn][20], a[20], b[20]; //货币名称
double x; //读入的汇率
double maxdist[maxn]; //源点i到其他每个顶点(包括它本身)的最长路径长度
int flag; //是否存在套汇的标志,flag=1表示存在
     int kase = 0; //测试数据序号
int readcase( ) //读入数据
{
    scanf( "%d", &n );
    if( n==0 ) return 0;
    for( i=0; i<n; i++ ) //读入n个货币名称
        scanf( "%s", name[i] );
    scanf( "%d", &m );
    for( 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) );//初始化maxdis[]数组
    maxdist[v0] = 1;
    for( k=1; 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( )
{
    while( readcase( ) ) //读入货币种类的数目
    {
        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/AXuan_K/article/details/40154909
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞