Poj_2240 Arbitrage(最短路)

题意:

套利就是利用汇率的差异使自己的财富得到增加。给出几种货币之间的汇率,问能否通过套利使得财富增加。

思路:

转换为寻找有向图中是否有环。使用Bellman-Ford和SPFA分别实现了一下,SPFA稍微快一点,注意SPFA是入队超过N次。

代码实现:

//Bellman-Ford
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <algorithm>

using namespace std;

const int MAX_V = 32;

struct Edge{
    int from;
    int to;
    double rate;
};

int N,M;
int cnt;
bool flag;
double curr[MAX_V];
map<string,int> mm;
Edge edge[MAX_V*MAX_V];
bool find_loop();
int main()
{
    cnt = 1;
    while( scanf("%d",&N) != EOF ){
        if( N == 0 ){
            break;
        }
        string a,b;
        mm.clear();
        for( int i = 0; i < N; i++ ){
            cin>>a;
            mm[a] = i+1;
        }
        double rate;
        scanf("%d",&M);
        for( int i = 0; i < M; i++ ){
            cin>>a>>rate>>b;
            edge[i].from = mm[a];
            edge[i].to = mm[b];
            edge[i].rate = rate;
        }
        flag = find_loop();
        if( flag == true ){
            printf("Case %d: Yes\n",cnt++);
        }
        else{
            printf("Case %d: No\n",cnt++);
        }
    }
    return 0;
}

bool find_loop(){
    memset(curr,0,sizeof(curr));
    curr[1] = 1;
    for( int i = 0; i < N; i++ ){
        for( int j = 0; j < M; j++ ){
            Edge e = edge[j];
            if( curr[e.to] < curr[e.from]*e.rate ){
                curr[e.to] = curr[e.from]*e.rate;
                if( i == N-1 ){
                    return true;
                }
            }
        }
    }
    return false;
}

//SPFA
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <vector>
#include <queue>

using namespace std;

const int MAX_V = 32;

struct Edge{
    int to;
    double rate;
    Edge(const int to,const double rate){
        this->to = to;
        this->rate = rate;
    }
};

int N,M;
int cnt;
bool flag;
int num[MAX_V];
queue<int> que;
bool inque[MAX_V];
map<string,int> mm;
double curr[MAX_V];
vector<Edge> edge[MAX_V];
int main(){
    cnt = 1;
    while( scanf("%d",&N) != EOF ){
        if( N == 0 ){
            break;
        }
        string a,b;
        mm.clear();
        for( int i = 0; i < MAX_V; i++ ){
            edge[i].clear();
        }
        for( int i = 0; i < N; i++ ){
            cin>>a;
            mm[a] = i+1;
        }
        double rate;
        scanf("%d",&M);
        for( int i = 0; i < M; i++ ){
            cin>>a>>rate>>b;
            int pa = mm[a];
            int pb = mm[b];
            edge[pa].push_back(Edge(pb,rate));
        }
        flag = false;
        memset(num,0,sizeof(num));
        memset(curr,0,sizeof(curr));
        memset(inque,false,sizeof(inque));
        curr[1] = 1;
        que.push(1);
        inque[1] = true;
        num[1]++;
        while( !que.empty() ){
            int tmp = que.front();
            que.pop();
            inque[tmp] = false;
            if( flag == true ){
                break;
            }
            int len = edge[tmp].size();
            for( int i = 0; i < len; i++ ){
                Edge e = edge[tmp][i];
                if( curr[e.to] < curr[tmp]*e.rate ){
                    curr[e.to] = curr[tmp]*e.rate;
                    if( inque[e.to] == false ){
                        inque[e.to] = true;
                        num[e.to]++;
                        que.push(e.to);
                        if( num[e.to] > N ){
                            flag = true;
                            break;
                        }
                    }
                }
            }
        }
        if( flag == true ){
            printf("Case %d: Yes\n",cnt++);
        }
        else{
            printf("Case %d: No\n",cnt++);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值