POJ 2240-Arbitrage(套汇-Bellman Ford)

Arbitrage

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20523 Accepted: 8740

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

题目意思:

套汇是利用汇率之间的差异,从而将一单位的某种货币,兑换回多于一单位的同种货币。 给出N种货币及其之间的汇率,判断是否能够进行套汇。

解题思路:

使用Bellman Ford,将最短路转换成最长边,依次从各个顶点出发,判断是否存在大于1的情况(可以套汇)。

Note:因为我用了string数组,所以G++可AC,C++会CE。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
#define INF 0xfffffff
#define MAXN 1010

struct Edge
{
    int u,v;
    double w;//汇率
};
Edge edge[MAXN];//邻接表
int n,m;//顶点数和边数
double dist[MAXN];//顶点s到其他顶点的最长路径
bool flag=false;

void Bellman(int s)//顶点s到其他顶点的最长路径
{
    int i,j;
    memset(dist,0,sizeof(dist));
    dist[s]=1.0;
    for(i=1; i<=n; ++i)
        for(j=0; j<m; ++j)
        {
            Edge e=edge[j];
            if(e.w*dist[e.u]>dist[e.v])//判断是否增加了最大距离
            {
                dist[e.v]=e.w*dist[e.u];
                //cout<<"dist["<<e.v<<"]="<<dist[e.v]<<" j="<<j<<endl;
            }
            if(dist[s]>1.0)//可以套汇
            {
                flag=true;
                return;
            }
        }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int i,j,cnt=0;
    while(cin>>n&&n)
    {
        string name[30];
        for(i=0; i<n; ++i)
            cin>>name[i];//保存货币名称
        cin>>m;
        for(i=0; i<m; ++i)
        {
            string a,b;
            double c;
            cin>>a>>c>>b;//读入汇率
            for(j=0; j<n; ++j)
            {
                if(a==name[j])
                    edge[i].u=j;
                if(b==name[j])
                    edge[i].v=j;
            }
            edge[i].w=c;
        }
        for(i=0; i<n; ++i)
        {
            flag=false;
            Bellman(i);//顶点i到其他顶点
            if(flag) break;
        }
        //cout<<"i="<<i<<" "<<dist[i]<<endl;
        if(flag) cout<<"Case "<<++cnt<<": "<<"Yes"<<endl;
        else cout<<"Case "<<++cnt<<": "<<"No"<<endl;
    }
    return 0;
}
/**
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
/**
3 3
0 1 0.5
1 2 10.0
2 0 0.21

3 6
0 1 0.5
0 2 4.9
1 2 10.0
1 0 1.99
3 1 0.09
2 0 0.19
**/
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/MIKASA3/article/details/52091567
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞