zoukankan      html  css  js  c++  java
  • 6.2.4 Arbitrage

    Arbitrage

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

    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

    思路:题目大意就是给你一张汇率表,问你从美元开始,能不能经过一轮的兑换,最终使你的钱增加 ,floyd求最长路(>1)

     1 #include <cstdio>
     2 #include <cstring>   
     3 #include <iostream>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <cstdlib>
     7 #include <map>
     8 using namespace std;
     9 
    10 const int maxn=100,INF=100000000.0;
    11 double d[maxn][maxn],x;
    12 int n,m;
    13 map<string,int> mp;
    14 
    15 char s[700],s1[700];
    16 
    17 void close()
    18 {
    19     exit(0);
    20 }
    21 
    22 void work()
    23 {
    24     for (int k=1;k<=n;k++)
    25         for (int i=1;i<=n;i++)
    26             for (int j=1;j<=n;j++)
    27                 if (d[i][k]*d[k][j]>d[i][j])
    28                     d[i][j]=d[i][k]*d[k][j];
    29     for (int i=1;i<=n;i++)
    30         if (d[i][i]>1)
    31         {
    32             printf("Yes\n");
    33             return;
    34         }
    35     printf("No\n");
    36 }
    37  
    38 
    39 void init()
    40 {
    41 int cas=0;
    42     while (scanf("%d",&n)!=EOF)
    43     {
    44         if (n==0) break;
    45         cas++;
    46         printf("Case %d: ",cas);
    47         /*
    48         for (int i=1;i<=n;i++)
    49             for (int j=1;j<=n;j++)
    50                 d[i][j]=-INF;
    51                 */
    52         for (int i=1;i<=n;i++)
    53         {
    54             d[i][i]=1;
    55             scanf("%s",s);
    56             mp[s]=i;
    57         }
    58         scanf("%d",&m);
    59         for (int i=1;i<=m;i++)
    60         {
    61             scanf("%s %lf %s",s,&x,s1);
    62             d[mp[s]][mp[s1]]=x;
    63         }
    64         work();
    65     }
    66 }
    67 
    68 
    69 int main ()
    70 {
    71     init();
    72     close();
    73 }
  • 相关阅读:
    POJ 1691 Painting a Board(状态压缩DP)
    POJ 1946 Cow Cycling(抽象背包, 多阶段DP)
    Leetcode: Best Time to Buy and Sell Stock I, II
    Leetcode: Valid Palindrome
    POJ 1946 Cow Cycling
    POJ 1661 Help Jimmy(递推DP)
    POJ 1160 Post Office(区间DP)
    POJ 2486 apple tree
    Android播放器框架分析之AwesomePlaye
    【开源框架】Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发,欢迎各位...
  • 原文地址:https://www.cnblogs.com/cssystem/p/3046011.html
Copyright © 2011-2022 走看看