Arbitrage
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 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 |
题意还是比较显然的,给你m种货币,给你m种货币兑换规则,问通过这些规则最后能不能盈利。
eg:1美元换0.5英镑,1英镑换10法郎,1法郎换0.21美元,这样1美元能换0.5*10*0.21=1.05美元,净赚0.05美元。
这道题和之前做的那题货币交换的还是比较相似的,所以也就用了一样的做法,用bellman-ford判正环。
把第一种货币置为1,然后跑一遍就可以了。
还有一个问题是如何存图。这里用一个map存每种货币对应一个坐标。
然后就可以了。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <math.h>
#include <queue>
#include <vector>
#include <map>
#include <climits>
using namespace std;
typedef long long ll;
const int maxn = 2e3 + 10;
const int inf = 0x01010101;
map<string,int> num;
struct edge
{
int from,to;
double cost;
edge(int a,int b,double c)
{
from = a;
to = b;
cost = c;
}
edge() {}
};
edge eg[maxn];
int total;
int n;
double money[maxn];
bool bellman_ford()
{
memset(money,0,sizeof(money));
money[1] = 1;
/*for(int i = 0; i < total; i++)
if(eg[i].from == 1)
money[eg[i].to] = eg[i].cost;*/
for(int i = 0; i < n; i++)
for(int j = 0; j < total; j++)
{
edge e = eg[j];
if(money[e.to] < money[e.from] * e.cost)
{money[e.to] = money[e.from] * e.cost;
if(i == n - 1) return true;}
}
return false;
}
int main()
{
// freopen("out.txt","w",stdout);
int cnt = 0;
while(cin>>n && n)
{
total = 0;
cnt++;
string s1,s2;
double fare;
for(int i = 1; i <= n; i++)
{
cin>>s1;
num[s1] = i;
}
int m;
cin>>m;
cin.get();
while(m--)
{
cin>>s1>>fare>>s2;
eg[total++] = edge(num[s1],num[s2],fare);
}
cout<<"Case "<<cnt<<": ";
if(bellman_ford())
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}