zoukankan      html  css  js  c++  java
  • POJ2240——Arbitrage(Floyd算法变形)

    Arbitrage

    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

    题目大意:

        有N种货币,给出了其中一些货币的汇率。eg:(A 2 B) 代表一块A能换两块B,但不代表两块B能换一块A(有向!)

        判断是否可以通过货币之间的转换来赚钱。样例一中的1单位USDollar可以通过转换变为1.05单位的USDollar,赚钱了!

    解题思路:

        Floyd算法的变形,Edge[i][j]=max(Edge[i][j],Edge[i][m]*Edge[m][j])

        初始化时将Edge[i][i]=1,Floyd之后通过判断Edge[i][i]是否大于1,来判断是否可以挣钱!

    Code:

     1 #include<string>
     2 #include<iostream>
     3 #include<stdio.h>
     4 #include<cstring>
     5 #define MAXN 100
     6 using namespace std;
     7 double edge[MAXN+10][MAXN+10];
     8 string name[MAXN+10];
     9 int N;
    10 void floyd()
    11 {
    12     for (int m=1; m<=N; m++)
    13         for (int i=1; i<=N; i++)
    14             for (int j=1; j<=N; j++)
    15                 if (edge[i][m]!=INT_MAX&&edge[m][j]!=INT_MAX&&  //Floyd的变形公式
    16                     edge[i][j]<edge[i][m]*edge[m][j])
    17                     edge[i][j]=edge[i][m]*edge[m][j];
    18 }
    19 int main()
    20 {
    21     int  M,times=0;
    22     while (cin>>N)
    23     {
    24         times++;
    25         for (int i=1; i<=N; i++)
    26             for (int j=1; j<=N; j++)
    27                 if (i!=j) edge[i][j]=INT_MIN;
    28                 else edge[i][j]=1;
    29         if (N==0) break;
    30         for (int i=1; i<=N; i++)
    31             cin>>name[i];
    32         cin>>M;
    33         for (int i=1; i<=M; i++)
    34         {
    35             string tmp2,tmp1;
    36             double dis;
    37             cin>>tmp1>>dis>>tmp2;
    38             int x1,x2;
    39             for (x1=1; x1<=N; x1++)    //将字符串转换成对应的标号
    40                 if (name[x1]==tmp1) break;
    41             for (x2=1; x2<=N; x2++)
    42                 if (name[x2]==tmp2) break;
    43             edge[x1][x2]=dis;
    44         }
    45         floyd();
    46         int cnt=0;
    47         for (int i=1;i<=N;i++)
    48             if (edge[i][i]>1)  //判断是否能通过各种转化挣钱
    49             cnt++;
    50         if (cnt>=1) printf("Case %d: Yes
    ",times);
    51         else printf("Case %d: No
    ",times);
    52     }
    53     return 0;
    54 }
  • 相关阅读:
    spring四种依赖注入方式
    java利用反射来调用一个类的私有方法
    IOC和AOP的基本概念
    开业大吉
    1752年9月奇怪的日历
    找到一个软件测试的学习网址,保留一下
    学习任务
    操作系统的第一次作业
    答题
    第四章读后感
  • 原文地址:https://www.cnblogs.com/Enumz/p/3865661.html
Copyright © 2011-2022 走看看