POJ 1860 Currency Exchange (Bellman-Ford 找正环)

Currency Exchange

Time Limit: 1000MS
Memory Limit: 30000K
Total Submissions: 19715
Accepted: 7040

Description

Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency.
For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 – 0.39) * 29.75 = 2963.3975RUR.
You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B – numbers of currencies it exchanges, and real RAB, CAB, RBA and CBA – exchange rates and commissions when exchanging A to B and B to A respectively.
Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations.

Input

The first line of the input contains four numbers: N – the number of currencies, M – the number of exchange points, S – the number of currency Nick has and V – the quantity of currency units he has. The following M lines contain 6 numbers each – the description of the corresponding exchange point – in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=103.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102.
Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 104.

Output

If Nick can increase his wealth, output YES, in other case output NO to the output file.

Sample Input

3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00

Sample Output

YES

Source

Northeastern Europe 2001, Northern Subregion

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

题目大意:  有多种汇币,汇币之间可以交换,这需要手续费,当你用100A币交换B币时,A到B的汇率是29.75,手续费是0.39,那么你可以得到(100 – 0.39) * 29.75 = 2963.3975 B币。问s币的金额经过交换最终得到的s币金额数能否增加

题解:  第一次学bellman-ford算法做的题,借鉴了大部分人的思路, bellman-ford一般用来判负环,这里是用来找正环,一种货币相当于图上一个点, 一个站点相当于一个环(这里要求a对b和b对a都可以互换),因此我们如果可以找到一个正权回路并且正环可以无限松弛(松弛操作)说明必然会出现套汇

#include <cstdio>
#include <cstring>
int const MAXN = 101; //点的最大值
int num;   //图上边的数量
int n, m, s; // n表示货币种类数目(图上点数), m表示站点数目,s表示所持货币的种类
double v;  //v表示所持货币面额
struct Point  //站点结构,记录两种货币间的汇率和手续费
{
    int a, b; 
    double rab, cab;
}point[10010];
double maxgain[101];   //记录到原点的总权值

bool Bellman()
{
    bool flag;   //标记能否找到正权回路
    memset(maxgain,0,sizeof(maxgain));  //初始化maxgain的值为0
    maxgain[s] = v;         //起点初始化为自身值
    for(int i = 0; i < n; i++)  //枚举图上每个点
    {
        flag = false;   
        for(int j = 0; j < num; j++)  //枚举每条边,判断加入该边之后总值是否会变大
            if(maxgain[point[j].b] < point[j].rab * (maxgain[point[j].a] - point[j].cab))
            {
                maxgain[point[j].b] = point[j].rab * (maxgain[point[j].a] - point[j].cab);
                flag = true;
            }
        if(!flag) //若没找到正环,则说明无解,退出
            break;
    }
    //判断正环是否可达,若可达则说明有解(专业说是判断正环是否可以无限松弛)
    for(int i = 0; i < num; i++)
        if(maxgain[point[i].b] < point[i].rab * (maxgain[point[i].a] - point[i].cab))
            return true;
    return false;
}

int main()
{
    int c1, c2;
    double r12, c12, r21, c21;
    while(scanf("%d %d %d %lf", &n, &m, &s, &v) != EOF)
    {
        num = 0;
        for(int i = 0; i < m; i++)
        {
            scanf("%d %d %lf %lf %lf %lf", &c1, &c2, &r12, &c12, &r21, &c21); 
            point[num].a = c1;
            point[num].b = c2;
            point[num].rab = r12;
            point[num++].cab = c12;  
            point[num].a = c2;      //c2 -> c1的转换信息
            point[num].b = c1;
            point[num].rab = r21;
            point[num++].cab = c21;
        }
        if(Bellman())
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/Tc_To_Top/article/details/38322365
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞