uva 104 Arbitrage (DP + floyd)

uva 104 Arbitrage

Description Download as PDF

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 and earn tex2html_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.0890.06111 2 2.0 0.45

Sample Output

1 2 1 1 2 4 1 no arbitrage sequence exists

题目大意:套汇。给你n种货币相互之间的汇率,问你怎样转换货币能在最少次数内获得利润。(利润 > 0.01)。解题思路:.89就是0.89。他所求的不是最大利润,而是最少的获利交换次数。所以遍历交换次数,没个交换次数都算一次获利,当获利超过0.01,,递归输出当前路径。用一个三维的dp数据dp[i][j][s],i, j代表由货币i转换到货币j,s代表转换次数,记录的是该情况下的汇率。然后用类似floyd的方式来更新这个数组。;ll;const int N = 25;int n;double rec[N][N][N], dp[N][N][N];int temp[N];void printfPath(int i, int j, int s) {if (s == 0) {printf(“%d”, i);return;}printfPath(i, rec[i][j][s], s – 1);printf(” %d”, j);return;}void DP() {int cnt, flag = 0;for (int s = 2; s <= n; s++) {cnt = 0;for (int k = 1; k <= n; k++) {for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {if (dp[i][j][s] < dp[i][k][s – 1] * dp[k][j][1]) {dp[i][j][s] = dp[i][k][s – 1] * dp[k][j][1];rec[i][j][s] = k;}}}}for (int i = 1; i <= n; i++) {if (dp[i][i][s] – 1.0 > 0.01) {flag = 1;printfPath(i, i, s);printf(“\n”);break;}}if (flag) break;}if (!flag) printf(“no arbitrage sequence exists\n”);}void input() {memset(dp, 0, sizeof(dp));memset(rec, 0, sizeof(rec));for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {if (i == j) dp[i][j][1] = 1;else scanf(“%lf”, &dp[i][j][1]);}}}int main() {while (scanf(“%d”, &n) != EOF) {input();DP();}return 0;}

回避现实的人,未来将更不理想。

uva 104 Arbitrage (DP + floyd)

相关文章:

你感兴趣的文章:

标签云: