Arbitrage
Source : University of Ulm Internal Contest 1996 | |||
Time limit : 1 sec | Memory limit : 32 M |
Submitted : 231, Accepted : 115
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 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 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
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 0Sample Output
Case 1: Yes Case 2: No
这个是求最优解,因此弗洛里得也应该算是DP的,这题的提意如上,给你一系列两种货币之间的汇率,现在给出问题是,我们可不可以从倒卖汇率中牟利,即通过一圈的货币购买,最后可以实现1元买到一元以上的钱,多好啊!
解题思路:肯定是求任意两种货币(如果能兑换的话)求出从起始货币,转一圈所能买到的最大货币价值,如果大于1,那么就可以倒卖货率,否则是不行的。
#include <iostream>
#include <cstdio>
#include<cstring>
#include <string>
#include <map>
using namespace std;
const int MAX = 101;
const int MAXN = 31;
double mat[MAXN][MAXN];
map<string, int> m;
char c[MAX];
int size;
/*
*抽象出就是找出图中是否含有一个圈使他的边权成乘积大于1,存在则就可以
* 倒卖汇率,否则不行,floyad求任意两个点之间的最优解。稍微改动即可。
*/
void insert(string s) {
if (m.find(s) == m.end()) {
m[s] = size++;
}
}
void floyad(int n) {
double temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
temp = mat[i][k] * mat[k][j];
if (temp > mat[i][j]) {
mat[i][j] = temp;
}
}
}
}
}
bool get_result(int n) {
for (int i = 0; i < n; i++) {
if (mat[i][i] > 1.0)return true;
}
return false;
}
int main() {
int n, n1;
int cas = 1;
while (scanf(" %d", &n) && n) {
string s;
size = 0;
m.clear();
for (int i = 0; i < n; i++) {
scanf(" %s", &c);
s = c;
insert(s);
}
scanf(" %d", &n1);
memset(mat, 0, sizeof (mat));
int a, b;
double rate;
for (int i = 0; i < n1; i++) {
scanf(" %s", &c);
s = c;
a = m[s];
scanf(" %lf", &rate);
scanf(" %s", &c);
s = c;
b = m[s];
mat[a][b] = rate;
}
floyad(n);
if (get_result(n)) {
printf("Case %d: Yes\n", cas++);
} else {
printf("Case %d: No\n", cas++);
}
}
return 0;
}