码迷,mamicode.com
首页 > 其他好文 > 详细

Nyoj Arbitrage(Floyd or spfa or Bellman-Ford)

时间:2016-04-08 00:38:29      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

描述
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.

 
输入
The input file 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.
输出
For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".
样例输入
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
来源
NKOJ or 1996/97 Ulm Internal Contest
上传者
苗栋栋

 

题意:给出一些货币和货币之间的兑换比率,问是否可以使某种货币经过一些列兑换之后,货币值增加。举例说就是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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!