zoukankan      html  css  js  c++  java
  • HDU

    题目:

    Arbitrage

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3443    Accepted Submission(s): 1562


    Problem 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 file 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
     
      题意:给你一些外汇,以及外汇A可以换多少外汇B,现在问你能否存在一种相互转换的方式最终可以通过这种方式赚钱。
      这一题我用了spfa,刚开始觉得应该是求最短路,然后通过计数每一种外汇进队列的次数,超过2次的时候就跳出来,说明赚不了钱但是结果wa了,看了人家的代码,别人是求最长路,但当第一种外汇有赚的时候就跳出来,说明可以赚钱。
      为什么可以这样做,我的理解是这样:
      首先,如果算最短路,通过看进队次数来判断可不可以赚钱这个应该用不上可能还没经过可以大幅度增加金额的点就已经在某些点进队了限定次数然后跳出来。
      其次,求最短路的话不想是存在负环的情况,虽然遇到小于1的小数会减小外汇,但是外汇减少的极限是0,不会变成负数,所以用判断负环的方法好像行不通。
      /*
      再者,为什么只需要判断第一种外汇有赚就可以跳出来,因为如果第一种外汇有赚,说明第一种外汇是在一个正环上的,那么现在的spfa是在求最长路,有正环那就一定会不断增加,换而言之,其实这里应该可以不判断第一种外汇的情况,可以判断其他外汇的情况。(这种说法有待考究,到底是数据太弱还是题目默认是求第一种外汇呢?)
      */
     
    代码:
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <utility>
    #include <queue>
    #include <map>
    #define MAX 50
    using namespace std;
    
    map<string,int> M;
    
    int n;
    double s[MAX][MAX];
    double dis[MAX];
    
    bool spfa()
    {
        int i,u;
        int vin[MAX];
        queue<int> q;
        memset(vin,0,sizeof(vin));
        memset(dis,0,sizeof(dis));
        dis[1]=1;
        q.push(1);
        vin[1]=1;
        while(!q.empty())
        {
            u=q.front();
            q.pop();
            vin[u]=0;
            for(i=1;i<=n;i++)
            {
                if(s[u][i]!=0)
                {
                    if(dis[i]<dis[u]*s[u][i])
                    {
                        dis[i]=dis[u]*s[u][i];
                        if(!vin[i])
                        {
                            q.push(i);
                            vin[i]=1;
                        }
                        if(dis[1]>1) return 1;
                    }
                }
            }
        }
        return 0;
    }
    
    int main()
    {
        string s1,s2;
        int i,m,x,y,t;
        double d;
        char c[100];
        t=1;
        //freopen("data.txt","r",stdin);
        while(scanf("%d",&n),n)
        {
            printf("Case %d: ",t++);
            M.clear();
            memset(s,0,sizeof(s));
            for(i=1;i<=n;i++)
            {
                scanf("%s",c);
                s1=c;
                M.insert(pair<string,int>(s1,i));
            }
            scanf("%d",&m);
            while(m--)
            {
                scanf("%s %lf",c,&d);
                s1=c;
                scanf("%s",c);
                s2=c;
                x=M[s1];
                y=M[s2];
                s[x][y]=d;
            }
            if(spfa()) printf("Yes
    ");
            else printf("No
    ");
        }
        return 0;
    }
    1217
  • 相关阅读:
    华为手机wifi调试adb,断开数据线offlin
    appium 识别抖音视频已经播放完成
    对于学习新知识的一点自我反思
    部分软件激活
    AndroidStudio 创建简单的app
    App 逆向思路
    链家
    pyqt5 截屏
    3.无重复字符的最长子串
    1.两数之和
  • 原文地址:https://www.cnblogs.com/sineatos/p/3297623.html
Copyright © 2011-2022 走看看