Arbitrage(bellman,最长路径)

来源:互联网 发布:mac apowersoft 编辑:程序博客网 时间:2024/05/22 02:02

Arbitrage
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20181 Accepted: 8574
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

Sample Output
Case 1: Yes
Case 2: No

Source
Ulm Local 1996

题意:
给出n种货币的兑换汇率,请问是否存在某种货币通过兑换后可以增值。即从1变的大于1;

分析:
货币通过各种兑换后回到自己,就像是走了一个回路一样。我们将一种货币i与另一种货币j的兑换汇率cij,可以看成是从i到j权值为cij的一条有向边。则问题就转换成了从某一顶点(起始为1)出发,走过一环路后的最大值问题。
用bellman-ford算法。由于需要回到原点,所以更新时要考虑v0本身,路径长度不是权值之和,而是权值之积。
假设maxdis[i]为原点到其他点(包括原点)的最长路径,地推公式如下:(c(u,v)表示从u货币到v货币的汇率)
初始:maxdis[v]=0;maxdis[v0]=1;
递推:对于每条边(u,v),maxdis[k][v]=max{maxdis[k-1][v],maxdis[k-1][u]*c(u,v)} k=1,2,3…n.

AC代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>using namespace std;const int maxn = 50;const int maxm = 1000;struct Edge{    int ci,cj;    double cij;};Edge E[maxm];int n,m;char name[maxn][20];double maxdis[maxn];int flag=0;void bellman(int v0){    memset(maxdis,0,sizeof(maxdis));    maxdis[v0]=1;    for(int k=1;k<=n;k++)    {        for(int i=0;i<m;i++)        {            if(maxdis[E[i].ci]*E[i].cij>maxdis[E[i].cj])                maxdis[E[i].cj]=maxdis[E[i].ci]*E[i].cij;        }    }    if(maxdis[v0]>1.0)        flag=1;}int main(){    int cas = 1,a,b;    char u[20],v[20];    double h;    while(scanf("%d",&n)!=EOF && n)    {        for(int i=0;i<n;i++)        {            scanf("%s",name[i]);        }        scanf("%d",&m);        for(int i=0;i<m;i++)        {            scanf("%s %lf %s",u,&h,v);            for(a=0;strcmp(u,name[a]);a++);            for(b=0;strcmp(v,name[b]);b++);            E[i].ci=a;E[i].cj=b;E[i].cij=h;        }        flag=0;        for(int i=0;i<n;i++)        {            bellman(i);            if(flag) break;        }        if(flag)            printf("Case %d: Yes\n",cas++);        else            printf("Case %d: No\n",cas++);    }    return 0;}
1 0