zoukankan      html  css  js  c++  java
  • HDOJ 1217 Arbirage(最短路)

    Arbitrage

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


    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
     

    Source
     
        货比A 可以购买 X 货币B ,问能否通过一种路径,用X货币能买到比本身更多的货币。并没有说两种货币可以互相换,可以用Floyd算法变式,将加法换为乘法,不能交换的两种货币的权值设置为0,只要有一种货币与自己的最短路径大于1就表明能够实现套利了。
    #include<iostream>
    #include<string>
    #include<cstdio>
    #include<vector>
    #include<algorithm>
    #define MAX 105
    using namespace std;
    int n,t,cnt=0;
    vector<string>name;
    double Map[MAX][MAX],w;
    string str,sa,sb;
    int Find(string s){
       for(int i=0;i<name.size();i++)
        if(name[i]==s)return i;
    }
    int Floyd()
    {
        for(int k=0;k<n;k++)
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                    Map[i][j]=max(Map[i][j],Map[i][k]*Map[k][j]);
        for(int i=0;i<n;i++)
            if(Map[i][i]>1)
            return 1;
        return 0;
    }
    int main()
    {
    
        while(cin>>n,n)
        {   
            name.clear();
            for(int i=0;i<MAX;i++)
                for(int j=0;j<MAX;j++)
                    Map[i][j]=0;
            for(int i=1;i<=n;i++)
            {
                cin>>str;
                name.push_back(str);
            }
            cin>>t;
            while(t--)
            {
                cin>>sa>>w>>sb;
                Map[Find(sa)][Find(sb)]=w;
            }
            printf("Case %d: ",++cnt);
            printf(Floyd()?"Yes
    ":"No
    ");
    
        }
    
    
    }


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Hadoop- Cluster Setup
    Hadoop- Cluster Setup
    【网络协议】动态主机配置协议DHCP
    【网络协议】动态主机配置协议DHCP
    数据流(任务并行库 TPL)
    数据流(任务并行库 TPL)
    js数据存储.html
    对象操作(2).html
    对象操作(1).html
    对象forin循环.html
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4771094.html
Copyright © 2011-2022 走看看