zoukankan      html  css  js  c++  java
  • hdu 1217 Arbitrage (spfa)

    Arbitrage
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 10100    Accepted Submission(s): 4583

    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
     

    C/C++:

     1 #include <map>
     2 #include <queue>
     3 #include <cmath>
     4 #include <string>
     5 #include <cstring>
     6 #include <cstdio>
     7 #include <climits>
     8 #include <algorithm>
     9 #define INF 0xffffff
    10 using namespace std;
    11 
    12 map <string, int> my_stl_map;
    13 int n, m;
    14 double my_map[35][35], s1_s2;
    15 char my_string[105], my_s1[105], my_s2[105];
    16 
    17 bool my_spfa(int s)
    18 {
    19     queue <int> Q;
    20     while (!Q.empty()) Q.pop();
    21     int my_book[35] = {0};
    22     double my_dis[35] = {0};
    23     my_dis[s] = 1.0;
    24     my_book[s] = 1;
    25     Q.push(s);
    26     while (!Q.empty())
    27     {
    28         int q1 = Q.front();
    29         my_book[q1] = 0;
    30         for (int i = 1; i <= n; ++ i)
    31         {
    32             if (my_dis[q1] * my_map[q1][i] > my_dis[i])
    33             {
    34                 my_dis[i] = my_dis[q1] * my_map[q1][i];
    35                 if (my_dis[s] > 1.0) return true;
    36                 if (!my_book[i])
    37                 {
    38                     my_book[i] = 1;
    39                     Q.push(i);
    40                 }
    41             }
    42         }
    43         Q.pop();
    44     }
    45     return 0;
    46 }
    47 
    48 int main()
    49 {
    50     int my_cnt = 1;
    51 
    52     while (~scanf("%d", &n), n)
    53     {
    54         /**
    55             Initialize
    56         */
    57         my_stl_map.clear();
    58         for (int i = 1; i <= n; ++ i)
    59             for (int j = 1; j <= n; ++ j)
    60                 my_map[i][j] = i == j ? 1.0 : 0;
    61         /**
    62             Date Input
    63         */
    64         for (int i = 1; i <= n; ++ i)
    65         {
    66             scanf("%s", my_string);
    67             my_stl_map[my_string] = i;
    68         }
    69         scanf("%d", &m);
    70         for (int i = 1; i <= m; ++ i)
    71         {
    72             scanf("%s%lf%s", my_s1, &s1_s2, my_s2);
    73             my_map[my_stl_map[my_s1]][my_stl_map[my_s2]] = s1_s2;
    74         }
    75 
    76         bool my_flag = false;
    77         for (int i = 1; i <= n; ++ i)
    78             if (my_spfa(i))
    79             {
    80                 my_flag = true;
    81                 break;
    82             }
    83         if (my_flag) printf("Case %d: Yes
    ", my_cnt ++);
    84         else printf("Case %d: No
    ", my_cnt ++);
    85     }
    86     return 0;
    87 }
  • 相关阅读:
    【leetcode】1295. Find Numbers with Even Number of Digits
    【leetcode】427. Construct Quad Tree
    【leetcode】1240. Tiling a Rectangle with the Fewest Squares
    【leetcode】1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold
    【leetcode】1291. Sequential Digits
    【leetcode】1290. Convert Binary Number in a Linked List to Integer
    【leetcode】1269. Number of Ways to Stay in the Same Place After Some Steps
    【leetcode】1289. Minimum Falling Path Sum II
    【leetcode】1288. Remove Covered Intervals
    【leetcode】1287. Element Appearing More Than 25% In Sorted Array
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9436400.html
Copyright © 2011-2022 走看看