【题目描述】
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 USDollar3
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 USDollar0
Sample Output
Case 1: Yes
Case 2: No
【题目分析】
我们如果能够判断有一个环在增加就可以,因此我们可以用Bellman-Ford算法或者SPFA算法。
因为货币是字符串,用map比较好
跑出来的结果SPFA果然还是比Bellman-Ford快那么一点。
【AC代码】
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<climits>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;typedef long long ll;
const int MAXN=35;
const int MAXM=1e5+5;
map<string,int> name;
string t,t1,t2;
double r;
struct node
{int u;int v,next;double w;
}Edge[MAXM],E;
double dis[MAXN];
int vis[MAXN];
int times[MAXN];
int head[MAXN];
int tot;
int n,m;inline void AddEdge(int u,int v,double w)
{tot++;//Edge[tot].u=u;Edge[tot].v=v; Edge[tot].w=w;Edge[tot].next=head[u]; head[u]=tot;
}bool Bellman()
{memset(dis,0,sizeof(dis));dis[1]=1.0;int T=n; bool flag;while(T--){flag=false;for(int i=0;i<tot;i++){E=Edge[i];if(dis[E.v]<dis[E.u]*E.w){dis[E.v]=dis[E.u]*E.w;flag=true;}}if(!flag) break;}return T==-1;
}bool SPFA()
{memset(vis,0,sizeof(vis));memset(times,0,sizeof(times));memset(dis,0,sizeof(dis));queue<int> q;int x,v;dis[1]=1.0; q.push(1);vis[1]=true;while(!q.empty()){x=q.front(); q.pop(); vis[x]=false;for(int i=head[x];i;i=Edge[i].next){v=Edge[i].v;if(dis[v]<dis[x]*Edge[i].w){dis[v]=dis[x]*Edge[i].w;if(!vis[v]){q.push(v); vis[v]=true;times[v]++;if(times[v]>=n) return true;}}}}return false;
}int main()
{int Case=0;while(~scanf("%d",&n) && n){Case++;for(int i=1;i<=n;i++){cin>>t;name[t]=i;}scanf("%d",&m);tot=0;memset(head,0,sizeof(head));for(int i=1;i<=m;i++){cin>>t1>>r>>t2;AddEdge(name[t1],name[t2],r);}//if(Bellman())if(SPFA()){printf("Case %d: Yes\n",Case);}else{printf("Case %d: No\n",Case);}}return 0;
}