poj 2240 Arbitrage 题解
Posted SBSOI
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2240 Arbitrage 题解相关的知识,希望对你有一定的参考价值。
Arbitrage
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 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 |
[Submit] [Go Back] [Status] [Discuss]
——————————————————————————————我是分割线————————————————————————
思路好题。
最短路Bellman-Ford算法。
用图中的顶点代表货币。
若第i种货币能兑换成第j种,汇率为cij,则从顶点i至顶点j连一条有向边,权值为cij。
问题就转化成判断图中是否存在某个顶点,从它出发的某条回路上的权值乘积大于1。
大于1即有套汇。
1 /* 2 Problem:poj 2240 Arbitrage 3 OJ: POJ 4 User: S.B.S. 5 Time: 797 ms 6 Memory: 856 kb 7 Length: 1754 b 8 */ 9 #include<iostream> 10 #include<cstdio> 11 #include<cstring> 12 #include<cmath> 13 #include<algorithm> 14 #include<queue> 15 #include<cstdlib> 16 #include<iomanip> 17 #include<cassert> 18 #include<climits> 19 #include<functional> 20 #include<bitset> 21 #include<vector> 22 #include<list> 23 #include<map> 24 #define F(i,j,k) for(int i=j;i<=k;i++) 25 #define M(a,b) memset(a,b,sizeof(a)) 26 #define FF(i,j,k) for(int i=j;i>=k;i--) 27 #define maxn 10001 28 #define inf 0x3f3f3f3f 29 #define maxm 1001 30 #define mod 998244353 31 //#define LOCAL 32 using namespace std; 33 int read(){ 34 int x=0,f=1;char ch=getchar(); 35 while(ch<\'0\'||ch>\'9\'){if(ch==\'-\')f=-1;ch=getchar();} 36 while(ch>=\'0\'&&ch<=\'9\'){x=x*10+ch-\'0\';ch=getchar();} 37 return x*f; 38 } 39 struct EXCHANGE 40 { 41 int ca; 42 int cb; 43 double change; 44 }ex[maxn]; 45 char a[maxn],b[maxn]; 46 char name[maxn][21]; 47 double x; 48 double d[maxn]; 49 int ca=0,ans; 50 bool flag=false; 51 int n,m; 52 inline int init() 53 { 54 cin>>n; 55 if(n==0) return 0; 56 for(int i=0;i<n;i++) cin>>name[i]; 57 cin>>m; 58 for(int i=0;i<m;i++) 59 { 60 int j,k; 61 cin>>a;cin>>x;cin>>b; 62 for(j=0;strcmp(a,name[j]);j++); 63 for(k=0;strcmp(b,name[k]);k++); 64 ex[i].ca=j;ex[i].change=x;ex[i].cb=k; 65 } 66 return 1; 67 } 68 inline void ford(int u) 69 { 70 flag=false;M(d,0); 71 d[u]=1; 72 F(k,1,n)F(i,0,m-1){ 73 if(d[ex[i].ca]*ex[i].change>d[ex[i].cb]) 74 { 75 d[ex[i].cb]=d[ex[i].ca]*ex[i].change; 76 } 77 } 78 if(d[u]>1.0) flag=true; 79 } 80 int main() 81 { 82 std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y; 83 #ifdef LOCAL 84 freopen("data.in","r",stdin); 85 freopen("data.out","w",stdout); 86 #endif 87 while(init()){ 88 F(i,0,n-1){ 89 ford(i); 90 if(flag) break; 91 } 92 if(flag) cout<<"Case "<<++ca<<": "<<"Yes"<<endl; 93 else cout<<"Case "<<++ca<<": "<<"No"<<endl; 94 } 95 return 0; 96 }
以上是关于poj 2240 Arbitrage 题解的主要内容,如果未能解决你的问题,请参考以下文章