Arbitrage
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 Specification
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 cito 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 Specification
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
3USDollarBritishPoundFrenchFranc3USDollar 0.5 BritishPoundBritishPound 10.0 FrenchFrancFrenchFranc 0.21 USDollar 3USDollarBritishPoundFrenchFranc6USDollar 0.5 BritishPoundUSDollar 4.9 FrenchFrancBritishPound 10.0 FrenchFrancBritishPound 1.99 USDollarFrenchFranc 0.09 BritishPoundFrenchFranc 0.19 USDollar 0
Sample Output
Case 1: YesCase 2: No
题目类型:最长回路
算法分析:调用n次bellman-ford算法计算每一点为源点的最长回路(乘积),对于长度大于1.0的情况直接可以判断出出现套汇
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
#include <iostream> #include <fstream> #include <algorithm> #include <iomanip> #include <cstring> #include <cstdio> #include <cmath> #include <map> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <list> #include <ctime> using namespace std; const int maxn = 36 + 6; const double EPS = 1e-6; struct Node { int u, v; double w; }; Node edge[maxn*maxn]; char input[maxn][maxn]; double dis[maxn]; bool is_valid; int n, flag = 1, m; void bellman_ford (int s) { memset (dis, 0, sizeof (dis)); dis[s] = 1; int i; for (i = 1; i <= n; i++) { int j; for (j = 0; j < m; j++) { if (dis[edge[j].u] * edge[j].w > dis[edge[j].v] + EPS) dis[edge[j].v] = dis[edge[j].u] * edge[j].w; } } if (dis[s] > 1.0 + EPS) is_valid = true; } int main() { // freopen ("aaa.txt", "r", stdin); while (scanf ("%d", &n) != EOF) { if (n == 0) break; int i; for (i = 0; i < n; i++) scanf ("%s", input[i]); scanf ("%d", &m); for (i = 0; i < m; i++) { char temp_a[maxn], temp_b[maxn]; double temp_w; scanf ("%s %lf %s", temp_a, &temp_w, temp_b); int j; for (j = 0; strcmp (temp_a, input[j]); j++) ; edge[i].u = j; for (j = 0; strcmp (temp_b, input[j]); j++) ; edge[i].v = j; edge[i].w = temp_w; } is_valid = false; for (i = 0; i < n; i++) { bellman_ford (i); if (is_valid) break; } cout << "Case " << flag++ << ": "; if (is_valid) cout << "Yes" << endl; else cout << "No" << endl; } return 0; } |
- « 上一篇:zoj1091
- zoj1203:下一篇 »