标签:
描述
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.
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
Case 1: Yes Case 2: No
题意:给出一些货币和货币之间的兑换比率,问是否可以使某种货币经过一些列兑换之后,货币值增加。举例说就是1美元经过一些兑换之后,超过1美元。可以输出Yes,否则输出No。
AC代码:
1 #include <vector> 2 #include <map> 3 #include <set> 4 #include <algorithm> 5 #include <iostream> 6 #include <cstdio> 7 #include <cmath> 8 #include <cstdlib> 9 #include <string> 10 #include <cstring> 11 #include <queue> 12 using namespace std; 13 #define INF 0x3f3f3f3f 14 #define MAX 111 15 16 double mp[MAX][MAX]; 17 int n,m; 18 19 void floyd() 20 { 21 for(int k=1; k<=n; k++) 22 for(int i=1; i<=n; i++) 23 for(int j=1; j<=n; j++) 24 if(mp[i][j]< mp[i][k]*mp[k][j]) 25 mp[i][j]=mp[i][k]*mp[k][j]; 26 } 27 28 void init() 29 { 30 for(int i=1; i<=n; i++){ 31 for(int j=1; j<=n; j++){ 32 if(i==j) 33 mp[i][j]=1; 34 else 35 mp[i][j]=0; 36 } 37 } 38 } 39 40 int main() 41 { 42 int sum=0; 43 double rate; 44 char a[111],b[111],c[111]; 45 while(~scanf("%d",&n)&&n){ 46 init(); 47 map<string,int> mmp; 48 for(int i=1; i<=n; i++){ 49 scanf("%s",a); 50 mmp[a]=i; 51 } 52 scanf("%d",&m); 53 for(int i=1; i<=m; i++){ 54 scanf("%s%lf%s",b,&rate,&c); 55 int x=mmp[b]; 56 int y=mmp[c]; 57 mp[x][y]=rate; 58 //printf("%d\n",mp[x][y]); 59 } 60 floyd(); 61 int flag=0; 62 for(int i=1; i<=n; i++){ 63 //printf("%d\n",mp[i][i]); 64 if(mp[i][i]>1){ 65 flag=1; 66 break; 67 } 68 } 69 printf("Case %d: ",++sum); 70 printf("%s\n",flag ? "Yes" : "No"); 71 } 72 }
Nyoj Arbitrage(Floyd or spfa or Bellman-Ford)
标签:
原文地址:http://www.cnblogs.com/wangmengmeng/p/5366063.html