zoukankan      html  css  js  c++  java
  • POJ 2240

    Time Limit: 1000MS Memory Limit: 65536K

    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

    Bellman-Ford算法的应用。

    请参考:http://www.cnblogs.com/freezhan/p/3238968.html

    至于里面说的:

    注意:

              这里的松弛操作要循环 N 次才能过,
           书上的松弛操作一直都是 N-1 次
           对于为什么是 N 或者 N-1 次一直没有理解清楚

    是因为这题目会测试很坑的样例,比如:

    input:
    1
    a
    1
    a 1.5 a
    output:
    Yes

    这样是一条头尾都是自己的节点的边,如果不循环n次,会直接忽略这条边……这样答案就会出错。

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<map>
     4 using namespace std;
     5 struct Edge{
     6     int u,v;
     7     double r;
     8 }edge[5000];
     9 int n,m;
    10 double d[40];
    11 void init(int st)
    12 {
    13     for(int i=1;i<=n;i++) d[i]=0;
    14     d[st]=1;
    15 }
    16 void relax(int u,int v,double r){if(d[v] < d[u]*r) d[v]=d[u]*r;}
    17 bool bellman_ford(int st)
    18 {
    19     init(st);
    20     for(int i=1;i<=n;i++)
    21     {
    22         for(int j=1;j<=m;j++) relax(edge[j].u,edge[j].v,edge[j].r);
    23     }
    24     if(d[st]>1.0) return false;
    25     return true;
    26 }
    27 int main()
    28 {
    29     int kase=0;
    30     while(scanf("%d",&n) && n!=0)
    31     {
    32         map<string,int> currency;
    33         for(int i=1;i<=n;i++)
    34         {
    35             string str;
    36             cin>>str;
    37             currency[str]=i;
    38         }
    39         scanf("%d",&m);
    40         for(int i=1;i<=m;i++)
    41         {
    42             string str1,str2;double r_tmp;
    43             cin>>str1>>r_tmp>>str2;
    44             edge[i].u=currency[str1];
    45             edge[i].v=currency[str2];
    46             edge[i].r=r_tmp;
    47         }
    48         bool flag=true;
    49         for(int i=1;i<=n;i++)
    50         {
    51             if(bellman_ford(i)==false)
    52             {
    53                 flag=false;
    54                 break;
    55             }
    56         }
    57         if(flag==false) printf("Case %d: Yes
    ",++kase);
    58         else printf("Case %d: No
    ",++kase);
    59     }
    60 }
  • 相关阅读:
    eclipse中打断点debug无效
    Jenkins构建部署Maven项目
    自动生成实体类和xml
    session 控制单点登录
    eclipse team 没有svn
    easyui combobox 设置滚动条
    unkow jdbc driver : http://maven.apache.org
    easyui datagrid列显示图片
    mysql中计算日期整数差
    Map<String, Object>转Object,Object转 Map<String, Object>
  • 原文地址:https://www.cnblogs.com/dilthey/p/6804146.html
Copyright © 2011-2022 走看看