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
题目大意:套汇
套利是使用货币汇率的差异将一个单位的货币转换为多个相同的货币单位,例如1美元可以买0.5英镑,1英镑可以买10法郎,1法郎可以买0.21美元,然后聪明的人经过一些列兑换可以得到 1*0.5*10*0.21 = 1.05美元,盈利百分之5,你的工作就是判断是否能套汇成功(不得不说这个描述简介漂亮,没有一句废话)。
/
这道题有点类似以前做过的汇率问题,不过没有指定哪一个是自己持有的货币,估计可以是任意一种能变多都行,试一下吧,用每一个货币都试试,反正最多30种,也不算多
方法没错,不过消耗的时间比较多,应该是map比较耗时,以后小心map
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
#include<queue>
#include<set>
using namespace std;
map<string,int>q;
double mp[50][50];
int main()
{
int n,x,k=0;
string ss,s;
double w;
while(cin>>n)
{
if(n==0)
break;
int key=0;
memset(mp,0,sizeof(mp));
for(int i=0; i<n; i++)
{
cin>>s;
q[s]=i;
mp[i][i]=1;
}
cin>>x;
for(int i=0; i<x; i++)
{
cin>>s>>w>>ss;
mp[q[s]][q[ss]]=w;
}
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
for(int k=0; k<n; k++)
{
mp[j][k]=max(mp[j][k],mp[j][i]*mp[i][k]);
}
for(int i=0; i<n; i++)
if(mp[i][i]>1)
{
key=1;
break;
}
printf("Case %d: %s\n",++k,key?"Yes":"No");
}
return 0;
}
KDFHNJKSHVFKJSDHK
#include<algorithm>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<string>
#include<map>
#include<iostream>
using namespace std;
const int maxn = 50;
const int oo = 0xfffffff;
const double StartMoney = 100;
struct node
{
int y;
double rate;
node(int y, double r):y(y), rate(r){}
};
vector<node> G[maxn];
double v[maxn];
void Initialization(int s, int N)//初始化变量
{
for(int i=1; i<=N; i++)
v[i] = -oo;
v[s] = StartMoney;
}
int Spfa(int s)
{
queue<int> Q;
Q.push(s);
while(Q.size())
{
int i = Q.front();Q.pop();
int len = G[i].size();
for(int j=0; j<len; j++)
{
node q = G[i][j];
double k = v[i] * q.rate;
if(k > v[q.y])
{
v[q.y] = k;
Q.push(q.y);
}
}
if(v[s] > StartMoney)
return 1;
}
return 0;
}
int main()
{
int N, M, t=1;
while(scanf("%d", &N), N)
{
int i;
double r;
string A, B;
map<string, int> a;
for(i=1; i<=N; i++)
{
cin >> A;
a[A] = i;
}
scanf("%d", &M);
for(i=1; i<=M; i++)
{
cin >> A >> r >> B;
int x = a[A], y = a[B];
G[x].push_back(node(y, r));
}
for(i=1; i<=N; i++)
{
Initialization(i, N);
if(Spfa(i) == 1)
break;
}
if(i <= N)
printf("Case %d: Yes\n", t++);
else
printf("Case %d: No\n", t++);
for(i=1; i<=N; i++)
G[i].clear();
}
return 0;