• (简单) POJ 2240 Arbitrage,SPFA。


      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.

      题目就是问货币能不能通过转换而让自己增加。。。

      用的SPFA来判断的环。。。枚举每一个点进行SPFA (也就是说floyd也是可以的。。。)。。。

    代码如下:

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    
    using namespace std;
    
    const int INF=10e8;
    const int MaxN=40;
    
    struct Edge
    {
        int v;
        double cost;
    
        Edge(int _v=0,double _cost=0):v(_v),cost(_cost) {}
    };
    
    vector <Edge> E[MaxN];
    bool vis[MaxN];
    int couNode[MaxN];
    
    bool SPFA(double lowcost[],int n,int start)
    {
        queue <int> que;
        int u,v;
        double c;
        int len;
    
        for(int i=1;i<=n;++i)
        {
            vis[i]=0;
            couNode[i]=0;
            lowcost[i]=0;
        }
    
        vis[start]=1;
        couNode[start]=1;
        lowcost[start]=1;
    
        que.push(start);
    
        while(!que.empty())
        {
            u=que.front();
            que.pop();
    
            vis[u]=0;
            len=E[u].size();
    
            for(int i=0;i<len;++i)
            {
                v=E[u][i].v;
                c=E[u][i].cost;
    
                if(lowcost[u]*c>lowcost[v])
                {
                    lowcost[v]=lowcost[u]*c;
    
                    if(!vis[v])
                    {
                        vis[v]=1;
                        ++couNode[v];
                        que.push(v);
    
                        if(couNode[v]>=n)
                            return 0;
                    }
                }
            }
        }
    
        return 1;
    }
    
    inline void addEdge(int u,int v,double c)
    {
        E[u].push_back(Edge(v,c));
    }
    
    char ss[40][100];
    double ans[MaxN];
    int N;
    
    int find(char *s)
    {
        for(int i=1;i<=N;++i)
            if(strcmp(s,ss[i])==0)
                return i;
    }
    
    int main()
    {
        int M;
        bool ok;
        char ts1[100],ts2[100];
        int t1,t2;
        double tr;
        int cas=1;
    
        for(scanf("%d",&N);N;scanf("%d",&N),++cas)
        {
            for(int i=1;i<=N;++i)
            {
                scanf("%s",ss[i]);
            
                E[i].clear();
            }
    
            scanf("%d",&M);
    
            for(int i=1;i<=M;++i)
            {
                scanf("%s %lf %s",ts1,&tr,ts2);
                t1=find(ts1);
                t2=find(ts2);
    
                addEdge(t1,t2,tr);
            }
    
            ok=0;
    
            for(int i=1;i<=N;++i)
                if(!SPFA(ans,N,i))
                {
                    ok=1;
                    break;
                }
    
            printf("Case %d: ",cas);
    
            if(ok)
                printf("Yes
    ");
            else
                printf("No
    ");
    
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    [转]QTP 怎样连接mysql数据库操作
    [转]使用Eclipse来开发Android源码
    组合排序
    插入排序
    冒泡排序
    选择排序
    计数排序
    希尔排序
    合并排序
    鸡尾酒排序
  • 原文地址:https://www.cnblogs.com/whywhy/p/4338532.html
Copyright © 2020-2023  润新知