zoukankan      html  css  js  c++  java
  • HDU 1217 Arbitrage(Floyd)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217

    Arbitrage

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


    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
     
    Recommend
    Eddy
     

    一开始看不出什么所以然来(最近一段时间没做题的缘故),后来看数据不大,想用搜索做,但是这貌似不太符合,果然提交后TLE了,之后想想,可以先用Floyd或Dijkstra处理下,然后再任意将两个相对的点对(这里理解为点对,把一种币看作一个点,与另一种币的汇率作为两个点的距离,相对的点对即相反的点对,如(0,1)和(1,0))相乘,如果结果大于1.0,说明符合要求,输出Yes,否则输出No,如,美元换港币的汇率是1.5,港币换美元的汇率是0.7,显然,二者之积大于1.0,因此输出Yes。

    注意最后输出的格式,"Case case:"后面有空格。。。。。

    贴下代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<math.h>
     5 #define exp 1e-9
     6 char mo[50][50];
     7 double g[50][50];
     8 int vis[50];
     9 int n, ok;
    10 int finds(char s[])
    11 {
    12     int i;
    13     for(i = 0; i < n; i++)
    14         if(strcmp(mo[i], s) == 0)return i;
    15 }
    16 void getdata()
    17 {
    18     int i, a, b, m;
    19     char s1[100], s2[100];
    20     double rate;
    21     for(i = 0; i < n; i++)
    22         scanf("%s", mo[i]);
    23     scanf("%d", &m);
    24     getchar();
    25     memset(g, 0, sizeof(g));
    26     while(m--)
    27     {
    28         scanf("%s %lf %s", s1, &rate, s2);
    29         a = finds(s1);
    30         b = finds(s2);
    31         g[a][b] = rate;
    32     }
    33 }
    34 void floyd()
    35 {
    36     int i, j, k;
    37     for(k = 0; k < n; k++)
    38         for(i = 0; i < n; i++)
    39             for(j = 0; j < n; j++)
    40             {
    41                 if(g[i][j] < g[i][k]*g[k][j])
    42                     g[i][j] = g[i][k] * g[k][j];
    43             }
    44 }
    45 void check()
    46 {
    47     int i, j;
    48     if(n == 1 && g[0][0] > 1.0)
    49     {
    50         printf("Yes\n");
    51         return ;
    52     }
    53     for(i = 0; i < n; i++)
    54         for(j = 0; j < n; j++)
    55         {
    56             if(i == j)continue;
    57             if(g[i][j]*g[j][i] > 1.0)
    58             {
    59                 printf("Yes\n");
    60                 return ;
    61             }
    62         }
    63     printf("No\n");
    64 }
    65 int main()
    66 {
    67     int cas = 0;
    68     while(scanf("%d", &n) != EOF && n)
    69     {
    70         getchar();
    71         getdata();
    72         printf("Case %d: ", ++cas);
    73         floyd();
    74         check();
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    如何进行有效沟通避免出现误会
    如何进行有效沟通
    怎样提高自己的团队合作能力
    javaScript简介
    css文本格式详解
    css简介及相关概念
    WebGL10---3D模型的加载与使用
    Canvas绘图与动画详解
    Canvas绘制时钟
    WebGL9----将canvas作为纹理,将动画作为纹理(2)
  • 原文地址:https://www.cnblogs.com/lfeng/p/3045769.html
Copyright © 2011-2022 走看看