POJ 2240-Arbitrage(套匯-Bellman Ford)

kewlgrl發表於2016-08-02
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


題目意思:

套匯是利用匯率之間的差異,從而將一單位的某種貨幣,兌換回多於一單位的同種貨幣。
給出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
**/


相關文章