zoukankan      html  css  js  c++  java
  • UVa 104

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=40

     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 earntex2html_wrap_inline29 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 ( tex2html_wrap_inline37 ) 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 tex2html_wrap_inline51 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个点时一样。

    参考代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #define N 25
     4 double dp[N][N][N],p[N][N][N];
     5 int n;
     6 
     7 void print_path(int i ,int j , int s)
     8 {
     9     if(s==0)
    10     {
    11         printf("%d",i);
    12         return ;
    13     }
    14     print_path(i, p[i][j][s] ,s-1);
    15     printf(" %d",j);
    16     return ;
    17 }
    18 void DP()
    19 {
    20     int s,m;
    21     for(s=2; s<=n; s++)
    22     {
    23         for(int k=1; k<=n; k++)
    24             for(int i=1; i<=n; i++)
    25                 for(int j=1; j<=n; j++)
    26                     if( dp[i][j][s] < dp[i][k][s-1]*dp[k][j][1])
    27                     {
    28                         dp[i][j][s]=dp[i][k][s-1]*dp[k][j][1];
    29                         p[i][j][s]=k;
    30                     }
    31         int i;
    32         for(i=1; i<=n; i++)
    33             if(dp[i][i][s]>1.01)
    34             {
    35                 m=i ;
    36                 break;
    37             }
    38         if(i<=n)
    39             break;
    40     }
    41     if(s>n)
    42         printf("no arbitrage sequence exists");
    43     else
    44         print_path(m , m , s);
    45 
    46     printf("
    ");
    47 }
    48 int main()
    49 {
    50     while(scanf("%d",&n)!=EOF)
    51     {
    52         memset(dp,0,sizeof(dp));
    53         memset(p,0,sizeof(p));
    54         for(int i=1; i<=n; i++)
    55             for(int j=1; j<=n; j++)
    56             {
    57                 if(i==j) dp[i][j][1]=1;
    58                 else
    59                     scanf("%lf",&dp[i][j][1]);
    60                 p[i][j][1]=j;
    61             }
    62         DP();
    63     }
    64     return 0;
    65 }
    具体分析推荐博客:http://www.cnblogs.com/scau20110726/archive/2012/12/26/2834674.html

  • 相关阅读:
    Python网络爬虫——bs4基本用法
    Python网络爬虫——requests模块(1)
    yii gii配置ip限制使用gii
    openfire连接数据库mysql
    js 提示条
    jquery滚动条平滑滑动
    yii2.0 添加组件baidu ueditor
    yii添加验证码 和重复密码
    css图标库 font-awesome.min.css
    yii配置访问路由权限配置
  • 原文地址:https://www.cnblogs.com/zpfbuaa/p/5041465.html
Copyright © 2011-2022 走看看