POJ2240 Floyd

来源:互联网 发布:中箭mod 知乎 编辑:程序博客网 时间:2024/06/06 00:45

Arbitrage
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18752 Accepted: 7929

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 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

题意:
给出几种货币和相应的兑换比例,问有没有可能经过一轮兑换后价值变高。
题解:
货币是英文,所以先放到一个map里给它编上号。然后用floyd计算出任意两点的距离。最后看看对角线的点的汇率有没有超过1.0就好。
*关于floyd算法,dp[i][j]表示的是第i个点到第j个点的距离。那么可以知道转移为dp[i][k] = max(dp[i][j]+dp[j][k],dp[i][k]).(本题是dp[i][j]*dp[j][k])循环方向为最外层为j,然后是i和k。

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>#include <queue>#include <vector>#define f(i,a,b) for(i = a;i<=b;i++)#define fi(i,a,b) for(i = a;i>=b;i--)#define M -0x3f3f3f3fusing namespace std;int n;map<string,int>List;//double G[100][100];double dp[100][100];int main(){//    freopen("data.in","r",stdin);    int CNT = 1;    while(~scanf("%d",&n)&&n){        int i,j,k;        string s;        memset(dp,0,sizeof(dp));        f(i,1,n){            cin>>s;            List[s] = i;            dp[i][i] = 1.0;        }        int m;        scanf("%d",&m);        f(i,1,m){            string s1,s2;            double t;            cin>>s1>>t>>s2;            int a = List[s1],b = List[s2];            dp[a][b] = t;        }        f(j,1,n)            f(i,1,n)                f(k,1,n)                        dp[i][k] = max(dp[i][k],dp[i][j]*dp[j][k]);        int ok = 0;        f(i,1,n)            if(dp[i][i]>1.0){                ok = 1;                break;            }        if(ok)            printf("Case %d: Yes\n",CNT++);        else            printf("Case %d: No\n",CNT++);    }    return 0;}
0 0
原创粉丝点击