POJ 2240 Arbitrage (求负环)

Arbitrage

题目链接:

http://acm.hust.edu.cn/vjudge/contest/122685#problem/I

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

Hint



题意:


求货币经过一系列兑换操作后能否升值.


题解:


转化为图模型后就是求满足条件的环是否存在.
这里把求最短路时的加法改成乘法即可,结果就是是否存在环使得路径大于1.
以下分别用三种方法求:
bellman-ford和floyd用时都较多,800+ms.
spfa只需要90+ms, 不过需要用c++交,否则TLE.(真是神奇)


代码:

spaf法:94ms (必须用c++交,否则TLE)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 1100
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int m,n,k;
int edges, u[maxn], v[maxn];
double w[maxn];
int first[maxn], _next[maxn];
double dis[maxn];

void add_edge(int s, int t, double val) {
    u[edges] = s; v[edges] = t; w[edges] = val;
    _next[edges] = first[s];
    first[s] = edges++;
}

queue<int> q;
bool inq[maxn];
int inq_cnt[maxn];
bool spfa(int s) {
    memset(inq, 0, sizeof(inq));
    memset(inq_cnt, 0, sizeof(inq_cnt));
    for(int i=1; i<=n; i++) dis[i] = 0; dis[s] = 1;
    while(!q.empty()) q.pop();
    q.push(s); inq_cnt[s]++;

    while(!q.empty()) {
        int p = q.front(); q.pop();
        inq[p] = 0;
        for(int e=first[p]; e!=-1; e=_next[e]) {
            double tmp = dis[u[e]] * w[e];
            if(dis[v[e]] < tmp) {
                dis[v[e]] = tmp;
                if(!inq[v[e]]) {
                    q.push(v[e]);
                    inq[v[e]] = 1;
                    inq_cnt[v[e]]++;
                    if(inq_cnt[v[e]] >= n) return 0;
                }
            }
        }
    }

    return 1;
}

map<string,int> name;

int main(int argc, char const *argv[])
{
    //IN;

    int ca = 1;
    while(scanf("%d", &n) != EOF && n)
    {
        memset(first, -1, sizeof(first));
        edges = 0;
        name.clear();

        for(int i=1; i<=n; i++) {
            string s; cin >> s;
            name.insert(make_pair(s, i));
        }
        cin >> m;
        for(int i=1; i<=m; i++) {
            string s,t; double w;
            cin>> s >> w >> t;
            int u = name.find(s)->second;
            int v = name.find(t)->second;
            add_edge(u,v,w);
        }

        if(!spfa(1)) printf("Case %d: Yes\n", ca++);
        else printf("Case %d: No\n", ca++);
    }

    return 0;
}
bellman-ford法:875ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 1100
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int m,n,k;
int edges, u[maxn], v[maxn];
double w[maxn];
int first[maxn], next[maxn];
double dis[maxn];

void add_edge(int s, int t, double val) {
    u[edges] = s; v[edges] = t; w[edges] = val;
    next[edges] = first[s];
    first[s] = edges++;
}

bool bellman(int s) {
    for(int i=1; i<=n; i++) dis[i]=0; dis[s] = 1;

    for(int i=1; i<=n; i++) {
        for(int e=0; e<edges; e++) {
            double tmp = dis[u[e]] * w[e];
            if(dis[v[e]] < tmp) {
                dis[v[e]] = dis[u[e]] * w[e];
                if(i == n) return 0;
            }
        }
    }

    return 1;
}

map<string,int> name;

int main(int argc, char const *argv[])
{
    //IN;

    int ca = 1;
    while(scanf("%d", &n) != EOF && n)
    {
        memset(first, -1, sizeof(first));
        edges = 0;
        name.clear();

        for(int i=1; i<=n; i++) {
            string s; cin >> s;
            name.insert(make_pair(s, i));
        }
        cin >> m;
        for(int i=1; i<=m; i++) {
            string s,t; double w;
            cin>> s >> w >> t;
            int u = name.find(s)->second;
            int v = name.find(t)->second;
            add_edge(u,v,w);
        }

        if(!bellman(1)) printf("Case %d: Yes\n", ca++);
        else printf("Case %d: No\n", ca++);
    }

    return 0;
}
floyd法:875ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 35
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int m,n,k;
double dis[maxn][maxn];

void floyd() {
    for(int k=1; k<=n; k++)
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
        if(dis[i][j] < dis[i][k]*dis[k][j])
            dis[i][j] = dis[i][k] * dis[k][j];
}

map<string,int> name;

int main(int argc, char const *argv[])
{
    //IN;

    int ca = 1;
    while(scanf("%d", &n) != EOF && n)
    {
        name.clear();
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                dis[i][j] = (i==j? 1.0:inf);

        for(int i=1; i<=n; i++) {
            string s; cin >> s;
            name.insert(make_pair(s, i));
        }
        cin >> m;
        for(int i=1; i<=m; i++) {
            string s,t; double w;
            cin>> s >> w >> t;
            int u = name.find(s)->second;
            int v = name.find(t)->second;
            dis[u][v] = w;
        }

        floyd();

        int flag = 1;
        for(int i=1; i<=n; i++)
            if(dis[i][i] > 1.0) {flag = 0;break;}

        if(!flag) printf("Case %d: Yes\n", ca++);
        else printf("Case %d: No\n", ca++);
    }

    return 0;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5751517.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值