Bellman-Ford||SPFA-POJ-2240-Arbitrage

Arbitrage
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18350 Accepted: 7772
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

又是那啥套利问题,和之前做的POJ1860基本一模一样,也是找有没有正环,在换算方面还要更加简单,与之不同的考察点在于对于数据的存储,我用了map这一STL。
超时的同学注意了,用c++提交试试,我是g++超时而c++稳过。

//
// main.cpp
// 最短路练习-I-Arbitrage
//
// Created by 袁子涵 on 15/10/15.
// Copyright © 2015年 袁子涵. All rights reserved.
//

#include <iostream>
#include <string>
#include <map>

using namespace std;

map<string,double>STL;

int n,m;
string cur,a,b;
double c;

typedef struct edge
{
    string a,b;
    double c;
}Edge;
Edge eg[10000];

bool bellmanford()
{
    bool flag=0;
    for (int i=0; i<n; i++) {
        flag=0;
        for (int j=0; j<m; j++) {
            if (STL[eg[j].b]<STL[eg[j].a]*eg[j].c) {
                STL[eg[j].b]=STL[eg[j].a]*eg[j].c;
                flag=1;
            }
        }
        if (!flag) {
            break;
        }
    }
    for (int i=0; i<m; i++) {
        if (STL[eg[i].b]<STL[eg[i].a]*eg[i].c) {
            return 1;
        }
    }
    return 0;
}

int main(int argc, const char * argv[]) {
    int t=0;
    while (scanf("%d",&n)!=EOF && n!=0) {
        memset(eg, 0, sizeof(eg));
        t++;
        cin >> cur;
        STL[cur]=1;
        for (int i=1; i<n; i++) {
            cin >> cur;
            STL[cur]=0;
        }
        scanf("%d",&m);
        for (int i=0; i<m; i++) {
            cin >> a >> c >> b;
            eg[i].a=a;
            eg[i].b=b;
            eg[i].c=c;
        }
        if (bellmanford())
            printf("Case %d: Yes\n",t);
        else
            printf("Case %d: No\n",t);
        STL.clear();

        getchar();
    }
    return 0;
}

以下是用SPFA做的,g++提交770ms,而c++是110ms。

//
// main.cpp
// POJ-2240-Arbitrage
//
// Created by 袁子涵 on 15/12/2.
// Copyright © 2015年 袁子涵. All rights reserved.
//
// 110ms 272KB

#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#define INF 0x3f3f3f3f
#define MAXN 35

using namespace std;

int n,m;
map<string,double>num;
struct Edge
{
    int v;
    double cost;
    Edge(int _v=0,double _cost=0):v(_v),cost(_cost){}
};
vector<Edge>E[MAXN];
void addedge(int u,int v,double w)
{
    E[u].push_back(Edge(v,w));
}
bool vis[MAXN];
int cnt[MAXN];
double dist[MAXN];
bool SPFA()
{
    memset(vis, 0, sizeof(vis));
    memset(dist, 0, sizeof(dist));
    memset(cnt, 0, sizeof(cnt));
    vis[1]=1;
    queue<int>que;
    while (!que.empty())    que.pop();
    que.push(1);
    cnt[1]=1;
    dist[1]=1;
    while (!que.empty()) {
        int u=que.front();
        que.pop();
        vis[u]=0;
        for (int i=0; i<E[u].size(); i++) {
            int v=E[u][i].v;
            if (dist[v]<dist[u]*E[u][i].cost) {
                dist[v]=dist[u]*E[u][i].cost;
                if (!vis[v]) {
                    vis[v]=1;
                    que.push(v);
                    if (++cnt[v]>n) return 0;
                }
            }
        }
    }
    return 1;
}
int main(int argc, const char * argv[]) {
    int t=0;
    while (cin >> n && n!=0) {
        num.clear();
        for (int i=0; i<MAXN; i++)
            E[i].clear();
        t++;
        string tmp,a,b;
        double c;
        for (int i=1; i<=n; i++) {
            cin >> tmp;
            num[tmp]=i;
        }
        cin >> m;
        for (int i=1; i<=m; i++) {
            cin >> a >> c >> b;
            addedge(num[a], num[b], c);
        }
        if(SPFA())
            cout << "Case " << t << ": No" << endl;
        else
            cout << "Case " << t << ": Yes" << endl;
        getchar();
    }
    return 0;
}
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/Roy_Yuan/article/details/49157905
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞