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
大致题意
多组数据,每行第一行一个数字 n 表示货币种类,接下来 n 行表示货币名称,接下来一个数字 m 表示共有几种货币之间的兑换机制,每行三个数a ,b , c表示a可以兑换 a x b 个 c,问一顿骚操作是否可以无限兑换不断升值,换到比原来更多的钱
具体思路
最短路spfa的判断是否存在环,在这里就是多个map的字符串映射为数字的操作,也不是很难,题目一看思路还是很清晰的
#include<stdio.h>
#include<map>
#include<algorithm>
#include<queue>
#include<string.h>
#include<iostream>
using namespace std;
int n;
map<string,int>Map;
struct node
{
int to;
double cost;
};
vector<node>line[35];
double dis[35];
int flag[35],flagnum[35];
bool spfa()
{
queue<int>dui;
while(!dui.empty())
{
dui.pop();
}
dis[1]=1;
flag[1]=1;
flagnum[1]++;
dui.push(1);
while(!dui.empty())
{
int now=dui.front();
dui.pop();
flag[now]=0;
for (int i=0;i<line[now].size();i++)
{
//printf("%lf %lf %lf\n",dis[now],line[now][i].cost,dis[line[now][i].to]);
if (dis[now]*line[now][i].cost>dis[line[now][i].to])
{
dis[line[now][i].to]=dis[now]*line[now][i].cost;
if (flag[line[now][i].to]==0)
{
flag[line[now][i].to]=1;
dui.push(line[now][i].to);
flagnum[line[now][i].to]++;
if (flagnum[line[now][i].to]>n)
return true;
}
}
}
}
return false;
}
int main()
{
int num=1;
while (scanf("%d%*c",&n)&&n!=0)
{
Map.clear();
for (int i=1;i<=n;i++)
{
line[i].clear();
flag[i]=0;
flagnum[i]=0;
dis[i]=0;
char import[105];
scanf("%s%*c",&import);
Map[import]=i;
}
int m;
scanf("%d%*c",&m);
for (int i=1;i<=m;i++)
{
char from[105],to[105];
double cost;
scanf("%s %lf %s%*c",&from,&cost,&to);
node now;
now.to=Map[to];
now.cost=cost;
line[Map[from]].push_back(now);
}
if (spfa())
{
printf("Case %d: Yes\n",num);
}else
{
printf("Case %d: No\n",num);
}
num++;
}
return 0;
}