hdu 1217 Arbitrage (spfa)

本文介绍了一种算法,用于检测货币兑换市场中的盈利机会。通过构建货币兑换率图,算法可以判断是否存在一个循环路径,使得从一种货币开始,经过一系列货币兑换后,能够以超过初始金额的同种货币结束,从而实现利润。该算法使用SPFA(Shortest Path Faster Algorithm)进行最短路径计算,特别适用于存在负权重边的情况。
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\n", my_cnt ++);
84         else printf("Case %d: No\n", my_cnt ++);
85     }
86     return 0;
87 }

 

转载于:https://www.cnblogs.com/GetcharZp/p/9436400.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值