poj 2240 Arbitrage(floyd/bellman判正圈)

来源:互联网 发布:淘宝卖家手续费 编辑:程序博客网 时间:2024/05/24 03:21

题目链接
Arbitrage
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 21236 Accepted: 9046

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. 

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

3USDollarBritishPoundFrenchFranc3USDollar 0.5 BritishPoundBritishPound 10.0 FrenchFrancFrenchFranc 0.21 USDollar3USDollarBritishPoundFrenchFranc6USDollar 0.5 BritishPoundUSDollar 4.9 FrenchFrancBritishPound 10.0 FrenchFrancBritishPound 1.99 USDollarFrenchFranc 0.09 BritishPoundFrenchFranc 0.19 USDollar0

Sample Output

Case 1: YesCase 2: No

Source

Ulm Local 1996



题意:给定一些money以及之间的转化,询问最后能不能让某种money升值。


题解:

这一题是poj1860的削弱版,当然也可以类似用bellman判正圈做,同时这题有个简单的方法,因为数据比较小,用floyd就可以了。使用bellman时要注意建单向边!!!


floyd版本:

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<map>#include<string>using namespace std;const int maxn=30+10;double d[maxn][maxn];map<string,int> cnt;int main(){int n,m;int cas=0;while(~scanf("%d",&n)&&n){cnt.clear();memset(d,0,sizeof(d));for(int i=1;i<=n;i++) d[i][i]=1;string s;for(int i=1;i<=n;i++)cin >> s,cnt[s]=i;scanf("%d",&m);while(m--){string s1,s2;double cost;cin >> s1 >> cost >> s2;int p1=0,p2=0;map<string,int>::iterator it;for(it=cnt.begin();it!=cnt.end();++it){if(s1==it->first) p1=it->second;if(s2==it->first) p2=it->second;}d[p1][p2]=cost;}for(int k=1;k<=n;k++) for(int i=1;i<=n;i++)  for(int j=1;j<=n;j++)   if(d[i][j]<d[i][k]*d[k][j])   d[i][j]=d[i][k]*d[k][j];bool flag=false;for(int i=1;i<=n;i++)if(d[i][i]>1.0) flag=true;if(flag) printf("Case %d: Yes\n",++cas);else printf("Case %d: No\n",++cas);}}


bellman版本:

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<map>#include<queue>#include<string>using namespace std;const int maxn=30+5;map<string,int> cnt;int tol;int n,m;int head[maxn],vis[maxn],inq[maxn];double d[maxn];struct Edge{int to,next;double w;}edge[maxn*maxn];void addedge(int u,int v,double cost){edge[tol].to=v,edge[tol].w=cost,edge[tol].next=head[u];head[u]=tol++;}void init(){cnt.clear();tol=0;memset(head,-1,sizeof(head));}bool spfa(){memset(vis,0,sizeof(vis));memset(inq,0,sizeof(inq));memset(d,0,sizeof(d));d[1]=1;inq[1]=1,vis[1]=1;queue<int> q;q.push(1);while(!q.empty()){int u=q.front();q.pop();inq[u]=0;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(d[v]<d[u]*edge[i].w){d[v]=d[u]*edge[i].w;if(inq[v]) continue;inq[v]=1;vis[v]++;if(vis[v]>n) return true;q.push(v);}}if(d[1]>1)  return true;}return false;}int main(){int cas=0;while(~scanf("%d",&n)&&n){init();string s;for(int i=1;i<=n;i++)cin >> s,cnt[s]=i;scanf("%d",&m);while(m--){string s1,s2;double cost;cin >> s1 >> cost >> s2;int p1=0,p2=0;map<string,int>::iterator it;for(it=cnt.begin();it!=cnt.end();++it){if(s1==it->first) p1=it->second;if(s2==it->first) p2=it->second;}addedge(p1,p2,cost);//addedge(p2,p1,1.0/cost);}if(spfa()) printf("Case %d: Yes\n",++cas);else printf("Case %d: No\n",++cas);}}




















0 0
原创粉丝点击