您的位置:首页 > 其它

POJ 2240 Arbitrage (Bellman-Ford)

2014-07-31 15:54 561 查看
Arbitrage
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 15402Accepted: 6478
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

题目链接: http://poj.org/problem?id=2240

本题题解思路: 通过汇率关系构建有向图,之后问题转化为判断图中是否存在某个顶点,从它出发的某条回路上权值乘积大于1,

大于1则表示存在套汇,建图过程看代码,举个样例二的结果是:

1. c1 = 0 -> c2 = 1 r12 = 0.5

2. c1 = 0 -> c2 = 2 r12 = 4.9

3. c1 = 1 -> c2 = 2 r12 = 10.0

4. c1 = 1 -> c2 = 0 r12 = 1.99

5. c1 = 2 -> c2 = 1 r12 = 0.09

6. c1 = 2 -> c2 = 0 r12 = 0.19

#include <cstdio>
#include <cstring>
using namespace std;
int const MAXN = 35;        //图中点的最大值
int const MAXM = 900;       //图中边的最大值
char currency[MAXN][20];    //记录货币名的数组
char atemp[20], btemp[20];  //中间变量用来记录对应输入的货币(用来建图)
int nc, nex;                //nc表示货币种类数目,nex表示货币间的可兑换数目
double rate;                //汇率
double maxgain[MAXN];       //记录每个点的汇率可能达到的最大值
bool flag;                  //标记变量
int T = 0;                  //样例个数
struct Exchange             //兑换结构体
{
    int c1, c2;             //c1表示货币1,c2表示货币2,相当于图中两个点
    double r12;             //r12表示货币1对货币2的汇率,相当于图中一条边
}ex[MAXM];

int Readcase()              //读数据函数
{
    scanf("%d", &nc);
    if(nc == 0)
        return 0;
    for(int i = 0; i < nc; i++)
        scanf("%s",currency[i]);
    scanf("%d",&nex);
    for(int i = 0; i < nex; i++)   //这里的输入是建图的过程
    {
        int j, k;
        scanf("%s %lf %s",atemp, &rate, btemp);
        for(j = 0; strcmp(atemp,currency[j]); j++);  //找到与当前输入匹配的货币
        for(k = 0; strcmp(btemp,currency[k]); k++);  //及汇率记入到结构体中
        ex[i].c1 = j;
        ex[i].c2 = k;
        ex[i].r12 = rate;
    }
    return 1;

}

void Bellman(int v0)   //传入的v0为当前起点(贝尔曼福特算法实现函数)
{
    flag = false;
    memset(maxgain,0,sizeof(maxgain));  //初始化maxgain为0j
    maxgain[v0] = 1.0;    //第一个点对自己的汇率为1
    for(int i = 1; i <= nc; i++)  //从maxgain(0)一直递推到maxgain(nc)
        for(int j = 0; j < nex; j++) //每次加一条边  
            if(maxgain[ex[j].c1] * ex[j].r12 > maxgain[ex[j].c2]) //判断每条边,加入它能否使得结果更大
            {
                maxgain[ex[j].c2] = maxgain[ex[j].c1] * ex[j].r12; //可以则加入
                if(maxgain[v0] > 1.0)   //判断是否出现套汇因为没要求最大所以找到即可退出
                {
                    flag = true;
                    return;
                }
            }
}

int main()
{   
    while(Readcase())
    {
        for(int i = 0; i < nc; i++)
        {
            Bellman(i);             //分别从每个点开始判断能否套汇
            if(flag)
                break;
        }
        if(flag)
            printf("Case %d: Yes\n", ++T);
        else
            printf("Case %d: No\n", ++T);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: