1.题目描述:
Arbitrage
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7671 Accepted Submission(s): 3554
Problem 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 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.
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
Recommend
Eddy
2.题意概述:
在每种钱币间进行各种交换,最后换回自己如果能赚,那么就Yes,否则No
3.解题思路:
最开始看到乘法总想着学长说的转换成log,乘法就变成了加法,就变成了最短路问题,再用dijkstra来一遍,啧啧啧。
可!!注意,汇率可能小于1啊!如果转换成Log用dijkstra相当于有负权边!!
正解:考虑数据n < 30,完全可以n三方的Floyd直接算乘法,最后判断一下就好
或者,保留log思想,不过换能对负边处理的spfa(这个题解我下次再更新)
4.AC代码:
01 - Floyd
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 55
#define eps 1e-6
#define pi acos(-1.0)
#define e 2.718281828459
#define mod (int)1e9 + 7
using namespace std;
typedef long long ll;
double m[maxn][maxn], dis[maxn];
bool vis[maxn];
map<string, int> mp;
bool floyd(int n)
{
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
m[i][j] = max(m[i][j], m[i][k] * m[k][j]);
for (int i = 1; i <= n; i++)
if (m[i][i] > 1.0)
return true;
return false;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int kase = 1, n, k;
while (~scanf("%d", &n), n)
{
/*init*/
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
m[i][j] = 0.0;
mp.clear();
/*read*/
for (int i = 1; i <= n; i++)
{
char ch[maxn];
scanf("%s", ch);
mp[string(ch)] = i;
}
scanf("%d", &k);
for (int i = 0; i < k; i++)
{
char sta[maxn], ed[maxn];
double rate;
scanf("%s%lf%s", sta, &rate, ed);
int u = mp[string(sta)];
int v = mp[string(ed)];
m[u][v] = rate;
}
/*floyd*/
printf("Case %d: ", kase++);
if (floyd(n))
puts("Yes");
else
puts("No");
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}
02 - spfa+ln
再进一步考虑log函数单调性,乘积大于1,那么取log大于0,取负号呢小于0 那么思路出来了,如果出现不等比例,那么肯定存在最短路是负数,即判断负环问题,可以用spfa解决:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define N 33
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
struct node
{
int to;
double val;
node(int a, double b) { to = a; val = b; }
};
vector<node> vt[N];
double dis[N];
bool vis[N];
int in[N];
map<string, int> mp;
bool spfa(int sta, int n)
{
for (int i = 1; i <= n; i++)
{
dis[i] = 1e15;
vis[i] = 0;
in[i] = 0;
}
queue<int> q;
q.push(sta);
dis[sta] = 0;
vis[sta] = 1;
in[sta] = 1;
while (!q.empty())
{
int u = q.front();
q.pop();
vis[u] = 0;
int sz = vt[u].size();
for (int i = 0; i < sz; i++)
{
int v = vt[u][i].to;
double val = vt[u][i].val;
if (dis[v] > dis[u] + val)
{
dis[v] = dis[u] + val;
if (!vis[v])
{
in[v]++;
if (in[v] == n)
return 1; // 负环
vis[v] = 1;
q.push(v);
}
}
}
if (dis[u] < 0)
return 1; // 优化
}
return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n, k, kase = 1;
char ch[22];
while (~scanf("%d", &n), n)
{
mp.clear();
for (int i = 1; i <= n; i++)
{
scanf("%s", ch);
mp[ch] = i;
vt[i].clear();
}
scanf("%d", &k);
while (k--)
{
double t;
scanf("%s", ch);
int u = mp[ch];
scanf("%lf ", &t);
scanf("%s", ch);
int v = mp[ch];
vt[u].push_back(node(v, -1.0 * log(t)));
}
printf("Case %d: ", kase++);
bool flag = 0;
for (int i = 1; i <= n; i++)
if (!spfa(i, n))
{
flag = 1;
break;
}
if (!flag)
puts("Yes");
else
puts("No");
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}