Arbitrage
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14707 Accepted: 6203 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 USDollar0Sample Output
Case 1: YesCase 2: NoSource
解题报告POJ训练计划。先贴代码,省赛回来再补。#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define inf 99999999 using namespace std; struct nodes { char ch[100]; }node[35]; struct edges { int u,v,next; double w; }edge[1010]; int head[35],cnt,n,m; void add(int u,int v,double w) { edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; } int sfpa(int s) { int vis[35],i,j; double dis[35]; for(i=0;i<n;i++) { dis[i]=0; vis[i]=0; } dis[s]=1.0; vis[s]=1; queue<int >Q; Q.push(s); while(!Q.empty()) { int u=Q.front(); Q.pop(); vis[u]=0; for(i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(dis[v]<dis[u]*edge[i].w) { dis[v]=dis[u]*edge[i].w; if(dis[s]>1.0) return 1; if(!vis[v]) { Q.push(v); vis[v]=1; } } } } return 0; } int main() { int i,j,u,v,f,k=1; char str1[100],str2[100]; double c; while(cin>>n,n) { cnt=0; f=0; memset(edge,0,sizeof(edge)); memset(node,0,sizeof(node)); memset(head,-1,sizeof(head)); for(i=0;i<n;i++) { cin>>node[i].ch; } cin>>m; for(i=0;i<m;i++) { cin>>str1>>c>>str2; for(j=0;j<n;j++) { if(strcmp(str1,node[j].ch)==0) u=j; if(strcmp(str2,node[j].ch)==0) v=j; } add(u,v,c); } for(i=0;i<n;i++) { if(sfpa(i)) f=1; } cout<<"Case "<<k++<<": "; if(f) cout<<"Yes\n"; else cout<<"No\n"; } return 0; }
POJ训练计划2240_Arbitrage(SPFA)
最新推荐文章于 2019-03-29 02:08:35 发布