Arbitrage(bellman,最长路径)
2016-07-15 15:16
274 查看
Arbitrage
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20181 Accepted: 8574
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
Ulm Local 1996
题意:
给出n种货币的兑换汇率,请问是否存在某种货币通过兑换后可以增值。即从1变的大于1;
分析:
货币通过各种兑换后回到自己,就像是走了一个回路一样。我们将一种货币i与另一种货币j的兑换汇率cij,可以看成是从i到j权值为cij的一条有向边。则问题就转换成了从某一顶点(起始为1)出发,走过一环路后的最大值问题。
用bellman-ford算法。由于需要回到原点,所以更新时要考虑v0本身,路径长度不是权值之和,而是权值之积。
假设maxdis[i]为原点到其他点(包括原点)的最长路径,地推公式如下:(c(u,v)表示从u货币到v货币的汇率)
初始:maxdis[v]=0;maxdis[v0]=1;
递推:对于每条边(u,v),maxdis[k][v]=max{maxdis[k-1][v],maxdis[k-1][u]*c(u,v)} k=1,2,3…n.
AC代码:
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20181 Accepted: 8574
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
Ulm Local 1996
题意:
给出n种货币的兑换汇率,请问是否存在某种货币通过兑换后可以增值。即从1变的大于1;
分析:
货币通过各种兑换后回到自己,就像是走了一个回路一样。我们将一种货币i与另一种货币j的兑换汇率cij,可以看成是从i到j权值为cij的一条有向边。则问题就转换成了从某一顶点(起始为1)出发,走过一环路后的最大值问题。
用bellman-ford算法。由于需要回到原点,所以更新时要考虑v0本身,路径长度不是权值之和,而是权值之积。
假设maxdis[i]为原点到其他点(包括原点)的最长路径,地推公式如下:(c(u,v)表示从u货币到v货币的汇率)
初始:maxdis[v]=0;maxdis[v0]=1;
递推:对于每条边(u,v),maxdis[k][v]=max{maxdis[k-1][v],maxdis[k-1][u]*c(u,v)} k=1,2,3…n.
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> using namespace std; const int maxn = 50; const int maxm = 1000; struct Edge { int ci,cj; double cij; }; Edge E[maxm]; int n,m; char name[maxn][20]; double maxdis[maxn]; int flag=0; void bellman(int v0) { memset(maxdis,0,sizeof(maxdis)); maxdis[v0]=1; for(int k=1;k<=n;k++) { for(int i=0;i<m;i++) { if(maxdis[E[i].ci]*E[i].cij>maxdis[E[i].cj]) maxdis[E[i].cj]=maxdis[E[i].ci]*E[i].cij; } } if(maxdis[v0]>1.0) flag=1; } int main() { int cas = 1,a,b; char u[20],v[20]; double h; while(scanf("%d",&n)!=EOF && n) { for(int i=0;i<n;i++) { scanf("%s",name[i]); } scanf("%d",&m); for(int i=0;i<m;i++) { scanf("%s %lf %s",u,&h,v); for(a=0;strcmp(u,name[a]);a++); for(b=0;strcmp(v,name[b]);b++); E[i].ci=a;E[i].cj=b;E[i].cij=h; } flag=0; for(int i=0;i<n;i++) { bellman(i); if(flag) break; } if(flag) printf("Case %d: Yes\n",cas++); else printf("Case %d: No\n",cas++); } return 0; }
相关文章推荐
- WebGIS实现在线要素编辑之ArcGIS Server 发布Feature Service 过程解析
- 20. 利用Veeam ONE监控vSphere虚拟化平台(Veeam ONE安装及配置)
- jquery和thinkphp利用formData属性ajax上传并,添加水印,裁剪,实现预览
- Oracle SCN与检查点
- Spring里PropertyPlaceholderConfigurer类的使用
- P12证书和描述文件
- WebGIS实现在线要素编辑之ArcGIS Server 发布Feature Service 过程解析
- 计算两个时间的差 年龄计算
- python xlsxwriter vs openpyxl
- 决策树—学习心得
- linux 命令行使用wget下载百度云资源
- linux vim 快捷键
- 19. 试用vSphere 6(六):VCSA(6.0.0.2175370)的安装与配置
- Live Migrate 操作 - 每天5分钟玩转 OpenStack(42)
- Web service
- ios开发之View属性hidden, opaque, alpha, opacity的区别
- 字符串转int/float/double
- 安装CAD遇到的问题及解决方法
- 鼠标在导航栏移入移出的隐藏显示效果jquery
- GP学习(二)—Executing tools and Accesubg licensing0