传送门。
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 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
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
【题意】
现在你有一些钱,现在你可以将你的钱按照国际转化规则转换成其他的钱,并且没有手续费,问你能否经过转换将你的钱变多。
【分析】
题意挺有意思的不知道现实中可不可以。其实稍微分析一下就可以发现,这个数据量很小,可以直接暴力每个关系,然后不断的转换,然后判断你的钱是多了还是少了。如果多了就直接输出,否则的话就一直转换。你可能会问,一直转换不就死循环了吗?其实这个很容易解释,假使你每次转换钱都变少了,那么转换一次之后就不会转换了。因为这个变少的值我们是不会选择的。
【代码】
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<cmath>
#include<map>
#include<time.h>
#include<bits/stdc++.h>
using namespace std;
double win[35][35];
double now[35];
string a,b;
map<string,int>mat;
int main()
{
// freopen("in.txt","w",stdout);
int len;
int g_case = 1;
while(cin>>len,len)
{
cout<<"Case "<< g_case++ <<": ";
mat.clear();
memset(win,0,sizeof(win));
memset(now,0,sizeof(now));
for(int i = 1;i<=len;i++)
{
cin>>a;
mat[a] = i;
}
double xx;
int x;
cin>>x;
while(x--)
{
cin>>a>>xx>>b;
win[mat[a]][mat[b]] = xx;
}
now[1] = 1;
bool flag = true;
while(flag)
{
flag = false;
for(int i = 1;i<=len;i++)
for(int j = 1;j<=len;j++)
if(now[j]<now[i]*win[i][j])
{
flag = true;
now[j] = now[i]*win[i][j];
}
if(now[1]>1) break;
}
if(now[1]>1) puts("Yes");
else puts("No");
}
return 0;
}