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之间的字符串匹配需要的最少括号数,那么 如果区间左边是(或[,表示可以和右边的字符串匹配, ...
随机推荐
- Hadoop第8周练习—Pig部署及统计访问日志例子
:搭建Pig环境 :计算每个IP点击次数 内容 运行环境说明 1.1 硬软件环境 线程,主频2.2G,6G内存 l 虚拟软件:VMware® Workstation 9.0.0 build- ...
- Ink – 帮助你快速创建响应式邮件(Email)的框架
Ink 可以帮助你快速创建响应的 HTML 电子邮件,可工作在任何设备和客户端.这个 CSS 框架帮助您构建可在任何设备上阅读的 HTML 电子邮件.曾经需要你兼顾各种邮件客户端的日子一去不复返了,I ...
- LitePal + Gson + Volley的ORM框架尝试方案
为了紧跟技术潮流,目前的项目开始采用ORM的思想进行重新设计. 数据库采用轻量级ORM框架LitePal,Json解析采用Gson,网络框架采用Volley. 如果只是单纯的将这些第三方框架引进来,事 ...
- Linux内核知识
版本 linus树 Linux内核最初创始人--Linus Torvalds管理一个Linus树,linus树也称为主线(mainline).一般指的upstream,"上游",也 ...
- JS 对象属性相关--检查属性、枚举属性等
1.删除属性 delete运算符可以删除对象的属性 delete person.age //即person不再有属性age delete person['age'] //或者这样 delete只是断开 ...
- Array,List,Struct可能被大家忽略的问题
Q1: 首先定义一个结构 public struct MyStruct { public int T; } 定义一个泛型List来存放结构体,然后访问第一个元素去修改T,输出T: List<My ...
- javaScript一些函数包括调试方法(二)
Number():设法把括号里面的值,转换成一个数,转换不了为数字的话,就返回NaN. 注意:Number()函数,会拒绝任何包含,非数字字符的字符串(阿拉伯数字.一个有效的小数位.+.-符号是允许的 ...
- 概率论 --- Uva 11181 Probability|Given
Uva 11181 Probability|Given Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.acti ...
- Azure开发者任务之三:理解Azure应用程序(上)
作为Windows Azure的托管服务被设计和开发的应用程序由这两部分组成: 1,托管代码 2,XML配置文件 托管代码对应不同的角色 XML文件对应不同的配置设置 我们可以看一下下面这张图,它详细 ...
- 【百度SEO优化】如何让蜘蛛爬行你的网站
大家都知道,现在做网站简单,但是推广就比较困难了,可能一些商家引入投资,直接烧钱做广告来推广,但是对于一些小站长,是没有那么多资金的.因此我们就要懂得一些SEO优化的知识了,简单介绍一下: 怎么让百度 ...