Arbitrage(bellman,最长路径)
来源:互联网 发布:mac apowersoft 编辑:程序博客网 时间:2024/05/22 02:02
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代码:
#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;}
- Arbitrage(bellman,最长路径)
- Arbitrage(bellman-Ford)
- Arbitrage--(Bellman—Ford)
- POJ 2240-Arbitrage(套汇-Bellman Ford)
- POJ2240《Arbitrage》方法:bellman
- POJ-2240 -Arbitrage(Bellman)
- poj2240 Arbitrage(bellman)
- poj 2240 Arbitrage(Bellman-Ford算法学习)
- 暑假-最短路(Bellman-ford、spfa)-A - Arbitrage
- poj2240——Arbitrage(Bellman-Ford算法)
- poj2240&zoj1092 Arbitrage(Bellman-Ford)
- POJ 2240 Arbitrage Floyd || Bellman
- poj 2240 Arbitrage bellman ford
- poj 2240 Arbitrage(bellman-ford)
- poj 2240 Arbitrage(最短路径)
- hdu 1217 Arbitrage (Floyd + 最大路径)
- poj-2240-Arbitrage(Bellman-ford算法练习 + Floyd算法练习)
- poj2240(Floyd最短路的变种---最长路 )Arbitrage
- 计算两个时间的差 年龄计算
- P12证书和描述文件
- WebGIS实现在线要素编辑之ArcGIS Server 发布Feature Service 过程解析
- jquery和thinkphp利用formData属性ajax上传并,添加水印,裁剪,实现预览
- 20. 利用Veeam ONE监控vSphere虚拟化平台(Veeam ONE安装及配置)
- Arbitrage(bellman,最长路径)
- Unity集成内嵌网页插件-支持Android和IOS
- 【旧代码整理】uri路由处理的重要组成部分 Model_Route
- WebGIS实现在线要素编辑之ArcGIS Server 发布Feature Service 过程解析
- cython,加速python,保护代码(3):扩展文件 .pxd
- 四 skylable集群管理
- bootstrap模态框
- xml解析 SAX
- 剑指offer-5-面试36:数组中的逆序对(时间效率和空间效率的平衡)