题目链接:http://poj.org/problem?id=2240
Arbitrage
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 22913 | Accepted: 9694 |
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题意:货币兑换,货币兑换货币,能否增值。两种解法,因为不是单源,所以floyd更简单,spfa比较麻烦;
坑点就有一个,后台数据可能会出现1美元兑换2美元这种情况;
方法floyd(处理多源更方便):AC代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#define LL long long
#define eps 1e-8
using namespace std;
const int mod = 1e7+7;
const int inf = 0x3f3f3f3f;
const int maxn = 1e6 +10;
int n,m,ans;
double x,mp[50][50];
string a,b;
map<string,int>ma;
bool floyd()
{
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
if(mp[i][k]*mp[k][j]>mp[i][j])
mp[i][j]=mp[i][k]*mp[k][j];
if(mp[i][i]>1.0)
return 1;
}
return 0;
}
int main()
{
int fla=1;
while(~scanf("%d",&n)&&n)
{
ma.clear();
memset(mp,0,sizeof(mp));
for(int i=0; i<n; i++)
mp[i][i]=1.0;//初始化放在cin前面。我自己打的代码就一直wa在这。
for(int i=0; i<n; i++)
{
cin>>a;
ma[a]=i;
}
scanf("%d",&m);
for(int i=0; i<m; i++)
{
cin>>a>>x>>b;
if(mp[ma[a]][ma[b]]<x)//这道题没有这个坑点,这个if()可以不要。
mp[ma[a]][ma[b]]=x;
}
if(floyd())
printf("Case %d: Yes\n",fla++);
else
printf("Case %d: No\n",fla++);
}
return 0;
}
方法spfa(处理单源更方便)(虽然麻烦但是还是要锻炼一下代码能力的):AC代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#define LL long long
#define eps 1e-8
using namespace std;
const int mod = 1e7+7;
const int inf = 0x3f3f3f3f;
const int maxn = 1e6 +10;
int n,m,ans;
double x;
double mp[50][50];
double dis[50];
int vis[50];
int cnt[50];
string a,b;
map<string,int>ma;
queue<double>q;
bool spfa(int y)
{
while(!q.empty())
q.pop();
memset(cnt,0,sizeof(cnt));
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
dis[y]=1.0;
double v=1.0;
q.push(y);
vis[y]=1;
cnt[y]++;
while(!q.empty())
{
int f=q.front();
q.pop();
vis[f]=0;
for(int i=0; i<n; i++)
if(dis[i]+eps<dis[f]*mp[f][i])
{
dis[i]=dis[f]*mp[f][i];
if(dis[y]>v+eps)
return 1;
if(!vis[i])
{
vis[i]=1;
q.push(i);
cnt[i]++;
if(cnt[i]>n)
return 0;
}
}
}
return 0;
}
int main()
{
int fla=1;
while(~scanf("%d",&n)&&n)
{
ma.clear();
memset(mp,0,sizeof(mp));
for(int i=0; i<n; i++)
mp[i][i]=1.0;
for(int i=0; i<n; i++)
{
cin>>a;
ma[a]=i;
}
scanf("%d",&m);
for(int i=0; i<m; i++)
{
cin>>a>>x>>b;
mp[ma[a]][ma[b]]=x;
}
ans=0;
for(int i=0; i<n; i++)
if(spfa(i))//spfa是处理单源的,加一个i=0到n就成了多源的;
{
ans=1;
break;
}
if(ans) printf("Case %d: Yes\n",fla++);
else printf("Case %d: No\n",fla++);
}
return 0;
}