HDU1217 POJ2240 ZOJ1092 ArbitrageFloyd算法

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU1217 POJ2240 ZOJ1092 ArbitrageFloyd算法相关的知识,希望对你有一定的参考价值。

Arbitrage
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13735 Accepted Submission(s): 6272

Problem 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 file 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
University of Ulm Local Contest 1996

问题链接HDU1217 POJ2240 ZOJ1092 Arbitrage
问题简述:(略)
问题分析:Floyd算法的变种,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* HDU1217 POJ2240 ZOJ1092 Arbitrage */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

/* Floyd-Warshall算法:计算图中任意2点之间的最短距离
 * 复杂度:O(N×N×N)
 * 输入:n 全局变量,图结点数
 *      g 全局变量,邻接矩阵,g[i][j]表示结点i到j间的边距离
 * 输出:g 全局变量
 */
//const int INF = 0x3f3f3f3f;
const int N = 30 + 1;
int n;
double g[N][N];

void floyd()

    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                g[i][j] = max(g[i][j], g[i][k] * g[k][j]);


char c[N][N], s1[N], s2[N];

int getid(char s[])

    for (int i = 1; i <= n; i++)
        if (strcmp(s, c[i]) == 0) return i;
    return 0;


int main()

    int m, caseno = 0;
    while (~scanf("%d", &n) && n) 
        for (int i = 1; i <= n; i++)
            scanf("%s", c[i]);
        scanf("%d", &m);

        memset(g, 0, sizeof g);
        for (int i = 1; i <= n; i++) g[i][i] = 1.0;

        double w;
        for (int i = 1; i <= m; i++) 
            scanf("%s%lf%s", s1, &w, s2);
            g[getid(s1)][getid(s2)] = w;
        

        floyd();

        int i;
        for (i = 1; i <= n; i++)
            if (g[i][i] > 1) 
                printf("Case %d: Yes\\n", ++caseno);
                break;
            
        if (i > n)
            printf("Case %d: No\\n", ++caseno);
    

    return 0;

以上是关于HDU1217 POJ2240 ZOJ1092 ArbitrageFloyd算法的主要内容,如果未能解决你的问题,请参考以下文章

POJ2033 LA3078 HDU1508 ZOJ2202 AlphacodeDFS+DP

POJ1932 HDU1317 ZOJ1935 UVA10557 XYZZYSPFA+Floyd

POJ1323 LA2521 HDU1338 ZOJ1362 Game Prediction贪心

POJ2092 LA3157 HDU1347 ZOJ2250 Grandpa is Famous排序

POJ1598 ZOJ1315 HDU1606 UVA409 UVALive5493 Excuses, Excuses!文本

POJ1573 ZOJ1708 UVA10116 UVALive5334 HDU1035 Robot MotionDFS+BFS