UVa 104 - Arbitrage(Floyd动态规划)
Arbitrage |
Background
The use of computers in the finance industry has been marked with controversy lately as programmed trading -- designed to take advantage of extremely small fluctuations in prices -- has been outlawed at many Wall Street firms. The ethics of computer programming is a fledgling field with many thorny issues.
The Problem
Arbitrage is the trading of one currency for another with the hopes of taking advantage of small differences in conversion rates among several currencies in order to achieve a profit. For example, if $1.00 in U.S. currency buys 0.7 British pounds currency, £1 in British currency buys 9.5 French francs, and 1 French franc buys 0.16 in U.S. dollars, then an arbitrage trader can start with $1.00 and earn dollars thus earning a profit of 6.4 percent.
You will write a program that determines whether a sequence of currency exchanges can yield a profit as described above.
To result in successful arbitrage, a sequence of exchanges must begin and end with the same currency, but any starting currency may be considered.
The Input
The input file consists of one or more conversion tables. You must solve the arbitrage problem for each of the tables in the input file.
Each table is preceded by an integer n on a line by itself giving the dimensions of the table. The maximum dimension is 20; the minimum dimension is 2.
The table then follows in row major order but with the diagonal elements of the table missing (these are assumed to have value 1.0). Thus the first row of the table represents the conversion rates between country 1 and n-1 other countries, i.e., the amount of currency of country i ( ) that can be purchased with one unit of the currency of country 1.
Thus each table consists of n+1 lines in the input file: 1 line containing n and n lines representing the conversion table.
The Output
For each table in the input file you must determine whether a sequence of exchanges exists that results in a profit of more than 1 percent (0.01). If a sequence exists you must print the sequence of exchanges that results in a profit. If there is more than one sequence that results in a profit of more than 1 percent you must print a sequence of minimal length, i.e., one of the sequences that uses the fewest exchanges of currencies to yield a profit.
Because the IRS (United States Internal Revenue Service) notices lengthy transaction sequences, all profiting sequences must consist of n or fewer transactions where n is the dimension of the table giving conversion rates. The sequence 1 2 1 represents two conversions.
If a profiting sequence exists you must print the sequence of exchanges that results in a profit. The sequence is printed as a sequence of integers with the integer i representing the line of the conversion table (country i). The first integer in the sequence is the country from which the profiting sequence starts. This integer also ends the sequence.
If no profiting sequence of n or fewer transactions exists, then the line
no arbitrage sequence exists
should be printed.
Sample Input
3
1.2 .89
.88 5.1
1.1 0.15
4
3.1 0.0023 0.35
0.21 0.00353 8.13
200 180.559 10.339
2.11 0.089 0.06111
2
2.0
0.45
Sample Output
1 2 1
1 2 4 1
no arbitrage sequence exists
解题思路:
给出n种国家的货币汇率,一定金额的某种货币经过一系列汇率变换后再换成原来货币,金额增加了,求出这样的一个变换,要求变换步数最少。
Floyd变形,关于Floyd动态规划的理解。
状态转移方程:
f[k][i][j]=min(f[k-1][i][j],f[k-1][i][k]+f[k-1][k][j])
f[k][i][j]表示只经过前k个点(包括k),从i到j的最小值。当k从1到n时,就是从i到j的最小值。我们熟悉的用二维数组的写法实际上是对空间的一种压缩。
解释一下:
计算只经过前k个点,从i到j的最小值时,有两种情况需要考虑:经过第k个点和不经过第k个点。经过第k个点则距离应是从i到k的最小值和从k到j的最小值,两个最小值的路径都必须只经过前k-1个点(为什么是k-1而不是k,事实上他们两数值相同,因为起点和终点已经有第k个点,只是在dp的过程中先产生k-1,f[k][i][k]和f[k][k][j]有可能比f[k][i][j]的值晚计算出,就不能在计算f[k][i][j]时用到这两个值)。不经过k的点则距离与只经过前k-1个点时一样。
参考代码:
#include <cstdio>
#include <cstring>
#define N 25
double dp[N][N][N],p[N][N][N];
int n; void print_path(int i ,int j , int s)
{
if(s==)
{
printf("%d",i);
return ;
}
print_path(i, p[i][j][s] ,s-);
printf(" %d",j);
return ;
}
void DP()
{
int s,m;
for(s=; s<=n; s++)
{
for(int k=; k<=n; k++)
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
if( dp[i][j][s] < dp[i][k][s-]*dp[k][j][])
{
dp[i][j][s]=dp[i][k][s-]*dp[k][j][];
p[i][j][s]=k;
}
int i;
for(i=; i<=n; i++)
if(dp[i][i][s]>1.01)
{
m=i ;
break;
}
if(i<=n)
break;
}
if(s>n)
printf("no arbitrage sequence exists");
else
print_path(m , m , s); printf("\n");
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(dp,,sizeof(dp));
memset(p,,sizeof(p));
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
{
if(i==j) dp[i][j][]=;
else
scanf("%lf",&dp[i][j][]);
p[i][j][]=j;
}
DP();
}
return ;
}
具体分析推荐博客:http://www.cnblogs.com/scau20110726/archive/2012/12/26/2834674.html
UVa 104 - Arbitrage(Floyd动态规划)的更多相关文章
- uva 104 Arbitrage (DP + floyd)
uva 104 Arbitrage Description Download as PDF Background The use of computers in the finance industr ...
- UVA 104 Arbitrage
动态规划类似FLOYD dp[i][j][k] 表示第i个点经过K次到达j点能获得的最大利润 #include <map> #include <set> #include &l ...
- UVA 436 - Arbitrage (II)(floyd)
UVA 436 - Arbitrage (II) 题目链接 题意:给定一些国家货币的汇率.问是否能通过不断换货币使钱得到增长 思路:floyd,完事后推断一下有没有连到自己能大于1的情况 代码: #i ...
- UVa(247),Floyd做传递闭包
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- HDU 1217 Arbitrage (Floyd)
Arbitrage http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of ...
- POJ2240——Arbitrage(Floyd算法变形)
Arbitrage DescriptionArbitrage is the use of discrepancies in currency exchange rates to transform o ...
- Nyoj Arbitrage(Floyd or spfa or Bellman-Ford)
描述Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a curren ...
- UVa 12099 The Bookcase - 动态规划
题目大意 给定一些书,每个书有一个高度和宽度,然后将它们放到一个三层的书架里(要求每一层都不为空).定义书架的大小为每层最大的高度和 乘 每层宽度和的最大值.求最小的书架大小. 显然动态规划(直觉,没 ...
- UVa 1626 Brackets sequence (动态规划)
题意:用最少的括号将给定的字符串匹配,输出最优解.可能有空行. 思路:dp. dp[i][j]表示将区间i,j之间的字符串匹配需要的最少括号数,那么 如果区间左边是(或[,表示可以和右边的字符串匹配, ...
随机推荐
- lettuce webdriver 自动化测试---玩转BDD
行为驱动开发(BDD),依然高大上的矗立在远方,很少被人问津,一方面是BDD的思想不太容易理解,别一方面BDD的资料并不多.中文的资料就更少了. 之前增写过一篇<python BDD 框架之le ...
- PerfMap – 显示前端网站性能的热力图插件
PerfMap 是用来分析前端性能,基于资源定时 API 生成网页资源加载的前端性能热图.热图的颜色和第一个毫秒值指示在什么时候页面的图像加载完毕,这是用户体验的一个很好的指标.括号中的第二个值是浏览 ...
- Direct3D11学习:(七)绘图基础——彩色立方体的绘制
转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 在前面的几篇文章中,我们详细介绍了Direct3D渲染所需要的数学基础和渲染管道理论知识.从这篇文章开始,我们就 ...
- React中的PropTypes详解
propTypes用来规范props必须满足的类型,如果验证不通过将会有warn提示. React PropTypes的种类有: React.PropTypes.array // 队列 React.P ...
- static、final、static final 用法
1.使用范围:类.方法.变量.2.区别和联系:2.1.static 含义:静态的,被 static 修饰的方法和属性只属于类不属于类的任何对象.2.2.static 用法:2.2.1.static 可 ...
- 3D拓扑自动布局之Web Workers篇
2D拓扑的应用在电信网管和电力SCADA领域早已习以为常了,随着OpenGL特别是WebGL技术的普及,3D方式的数据可视化也慢慢从佛殿神堂步入了寻常百姓家,似乎和最近高档会所被整改为普通茶馆是一样的 ...
- codevs4247奇特的生物 解析报告
4247 奇特的生物 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 科学家们最近发现了一种奇怪的生物,它们每天长大一岁 ...
- sql 添加修改说明
--为字段a2添加描述信息 EXECUTE sp_addextendedproperty N'MS_Description', '性别', N'user', N'dbo', N'table', N'表 ...
- Java、Hibernate(JPA)注解大全
1.@Entity(name=”EntityName”) 必须,name为可选,对应数据库中一的个表 2.@Table(name=””,catalog=””,schema=””) 可选,通常和@Ent ...
- [moka同学代码]PHP初级知识:上传文件源码
1.目录结构