POJ - 2240 Arbitrage (Bellman-Ford )

Arbitrage

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 23813 Accepted: 10087

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

Source

Ulm Local 1996

[Submit]   [Go Back]   [Status]   [Discuss]

题意: 先给出一组数据,表示有几种货币,再给出货币之间交换原则。

     例如第一组数据表示,1美元兑换0.5英镑,1英镑兑换10法郎,1法郎兑换0.21美元。

     那么到最后就可以用1美元换到了0.5*10.0*0.21=1.05美元。

      问是否存在套汇现象?

思路:

    每一种货币代表一个顶点,汇率为两点之间边的权值,能够构造出一个有向图。

    问题就转换为判断图中是否存在某个顶点,从它出发的某条回路上权值乘积是否大于1,

    大于1则表示存在套汇,可以用Bellman-Ford 算法,求从原点出发,回到原点,经过路

     径长度(权值)之积是否大于1.

主要是货币是字符串不好处理,可以用两种方法

    1,比较一下,把字符串全部变成数字

   2,用map容器处理(具体也不是很明白)

#include<stdio.h>
#include<string.h>
#include<map>         //map容器头文件
#include<iostream>
using namespace std;

int n,m;
double e[100][100];

void floyd()
{
    int k,i,j;
    for(k=1; k<=n; k++)
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
                if(e[i][j]<e[i][k]*e[k][j]) //注意这里是乘积
                    e[i][j]=e[i][k]*e[k][j];
}

//方法一:字符串比较
void input()
{
    char s[50][100],a[100],b[100];
    double c;
    int i,j,k;
    for(i=1; i<=n; i++)
        scanf("%s",s[i]);
    scanf("%d",&m);
    for(i=1; i<=m; i++)
    {
        scanf("%s%lf%s",a,&c,b);
        for(j=1; j<=n; j++)          //把货币名称转换成数字
            if(!strcmp(a,s[j])) break;
        for(k=1; k<=n; k++)
            if(!strcmp(b,s[k])) break;
        e[j][k]=c;
    }
}

//方法二:map容器
/*void input()
{
    char a[30],b[30],s[30];
    double c;
    map<string,int>mi;  //定义map容器
    int i;
    for(i=1;i<=n;i++)
    {
        scanf("%s",s);
        mi[s]=i;      //把纸币名称转换成数字
    }
    scanf("%d",&m);
    for(i=1;i<=m;i++)
    {
        scanf("%s%lf%s",a,&c,b);
        e[mi[a]][mi[b]]=c;
    }
}*/

int main()
{
    int tt=1,i,j;
    while(~scanf("%d",&n)&&n)
    {
        int f=0;
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
                if(i==j) e[i][j]=1;   //注意这个地方不再是0和INF了,因为求的是乘积
                else e[i][j]=0;
        input();
        floyd();
        for(i=1; i<=n; i++)
        if(e[i][i]>1)       //若求出来大于1,则说明用1美元套汇,最后换了大于1美元,套汇成功
        {
            f=1;
            break;
        }
        if(f) printf("Case %d: Yes\n",tt++);
        else
            printf("Case %d: No\n",tt++);
    }
}
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/ling_bei/article/details/76178033
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞