POJ 1860 Currency Exchange

Description

Several currencyexchange points are working in our city. Let us suppose that each pointspecializes in two particular currencies and performs exchange operations onlywith these currencies. There can be several points specializing in the samepair of currencies. Each point has its own exchange rates, exchange rate of Ato B is the quantity of B you get for 1A. Also each exchange point has somecommission, the sum you have to pay for your exchange operation. Commission isalways collected in source currency. 
For example, if you want to exchange 100 US Dollars into Russian Rubles at theexchange point, where the exchange rate is 29.75, and the commission is 0.39you will get (100 - 0.39) * 29.75 = 2963.3975RUR. 
You surely know that there are N different currencies you can deal with in ourcity. Let us assign unique integer number from 1 to N to each currency. Theneach exchange point can be described with 6 numbers: integer A and B - numbersof currencies it exchanges, and real RAB, CAB, RBA andCBA - exchange rates and commissions when exchanging A to B andB to A respectively. 
Nick has some money in currency S and wonders if he can somehow, after someexchange operations, increase his capital. Of course, he wants to have hismoney 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 ofthe input contains four numbers: N - the number of currencies, M - the numberof exchange points, S - the number of currency Nick has and V - the quantity ofcurrency units he has. The following M lines contain 6 numbers each - thedescription 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 twodigits after the decimal point, 10-2<=rate<=102,0<=commission<=102
Let us call some sequence of the exchange operations simple if no exchangepoint is used more than once in this sequence. You may assume that ratio of thenumeric values of the sums at the end and at the beginning of any simplesequence of the exchange operations will be less than 104

Output

If Nick canincrease 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

 

 

 

将每一种货币看作是一个结点,每一个兑换点产生一个环。只要存在一个能不断使货币增多的回路,那么就可以满足题目要求(正环)。存在对环的检测,所以这里使用Bellman-Ford算法,构造最长路径,最后检测是否存在正环。

Bellman-Ford:http://blog.csdn.net/hermit_inwind/article/details/50432271


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
struct Edge
{
    int u,v;
    double r,c;
}edge[210];

double dis[105];
int n,m,s;
double v;


bool Bellman(int u)
{
    memset(dis,0,sizeof dis);
    dis[s]=v;
    
    bool flag=0;
    for (int i=1;i<n;i++)
    {
        for (int j=0;j<u;j++)
        {
            int a=edge[j].u , b=edge[j].v;
            double c=edge[j].c , r=edge[j].r;
            if (dis[b] < r * (dis[a] - c))
            {
                dis[b] = r * (dis[a] - c);
                flag=1;
            }
        }
        if (!flag)
            break;
    }
    for (int i=0;i<u;i++)
    {
        int a=edge[i].u , b=edge[i].v;
        double c=edge[i].c , r=edge[i].r;
        if (dis[b]< r * (dis[a] - c))
            return 1;
    }
    return 0;
}

int main()
{
    while (cin>>n>>m>>s>>v)
    {
        int u=0;
        for (int i=0;i<m;i++)
        {
            int a,b;
            double rab,cab,rba,cba;
            cin>>a>>b>>rab>>cab>>rba>>cba;
            edge[u].u=a;
            edge[u].v=b;
            edge[u].r=rab;
            edge[u++].c=cab;
            edge[u].u=b;
            edge[u].v=a;
            edge[u].r=rba;
            edge[u++].c=cba;
        }
        if (Bellman(u))
            cout<<"YES\n";
        else
            cout<<"NO\n";
    }
    return 0;
}

最后在检测正环的时候需要对存在的每一条边进行检测,此时所有路径已经构造出来,若出现经过每条路径后,货币的值继续上涨的情况,说明存在正环,满足题目要求。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值