Arbitrage
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 23891 | Accepted: 10116 |
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.
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.
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种钱币,以及彼此之间的兑换关系(单向),问我们能不能绕一圈以后使手里的钱更多
可以用钱与钱直接的关系构造一个邻接矩阵,不过比较特殊的是edges[i][i]都赋值为1(0 好像也可以) 根据最短路思想对每个点做搜索,最后看本身是不是大于1,如果存在,则输出YES。
也可以直接用 floyd 来做 最短路的思想能更清楚了\
P.S. 这题好像时间卡得很紧,居然我有一份代码直接1s擦边过(和队列声明的位置有关)
不过最快的还是floyd
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#define pb push_back
#define mp make_pair
#define LL long long
#define max3(x, y, z) max((x), max((y), (z)))
#define min3(x, y, z) min((x), min((y), (z)))
using namespace std;
const int INF = 0x3f3f3f3f;
int n, m;
double edge[50][50];
map<string, int> M;
bool SPFA(int be)
{
double dis[50];
int vis[50];
queue<int> Q;
for(int i = 1; i <= n; i ++){
dis[i] = -1;
vis[i] = 0;
}
dis[be] = 1;
Q.push(be);
vis[be] = 1;
while(!Q.empty()){
int now = Q.front();
Q.pop();
vis[now] = 0;
for(int i = 1; i <= n; i ++){
if(dis[i] < dis[now] * edge[now][i]){
dis[i] = dis[now] * edge[now][i];
if(dis[be] > 1) return true;
if(!vis[i]){
vis[i] = 1;
Q.push(i);
}
}
}
}
return false;
}
int main()
{
int mark;
int ncase = 1;
while(scanf("%d", &n) == 1){
if(!n) break;
mark = 0;
for(int i = 1; i <= n; i ++){
string str;
cin >> str;
M[str] = i;
}
memset(edge, 0, sizeof(edge));
for(int i = 1; i <= n; i ++){
edge[i][i] = 1;
}
scanf("%d", &m);
for(int i = 0; i < m; i ++){
string stra, strb;
double rate;
cin >> stra >> rate >> strb;
edge[M[stra]][M[strb]] = rate;
}
for(int i = 1; i <= n; i ++){
if(SPFA(i)){
mark = 1;
break;
}
}
printf("Case %d: ", ncase);
if(mark){
printf("Yes\n");
}
else{
printf("No\n");
}
ncase ++;
}
return 0;
}