码迷,mamicode.com
首页 > 其他好文 > 详细

poj1860(Bellman—fold)

时间:2018-03-30 19:56:51      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:res   nbsp   may   order   ring   http   问题   file   class   

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

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

题目大意:有若干种货币,若干个兑换点,每个兑换点可以把一种货币兑换为另一种货币(可A->B,也可B->A),但是兑换有佣金,假设把A变为B,汇率为r,佣金为c,则B=(A-c)*r。给出这些兑换点的信息 以及 初始的钱的种类和数量,求是否可能进过若干次兑换使钱(最后必须是最开始的币种)变多;
解题思路:转化为图,货币为节点,兑换点为边,则构成一个无向图,而问题就转化成了求次无向图是否存在正环(因为最后要化成开始的币种,而不是价值变多即可,所以是求正环)
用Bellman——fold算法的思想,可以无限松弛即为正环,就可以解决了(原算法为求负环,只需把初始化的状态和松弛条件改一下即可)

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>

using namespace std;

struct Edge
{
    int from,to;
    double r,c;
    Edge(int u,int v,double r,double c):from(u),to(v),r(r),c(c) {}
};

vector<int> G [205];
vector<Edge> edges;
int cnt[105]= {0};
bool inq[105]= {0};
double d[105];
int n;

bool bellman_fold(int s,double value)
{
    queue<int> Q;
    memset(d,0,sizeof(d));
    Q.push(s);
    d[s]=value;
    inq[s]=1;
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        inq[u]=0;
        for(int i=0; i<G[u].size(); i++)
        {
            int now=G[u][i];
            Edge & e=edges[now];
            if(d[u]>0&&d[e.to]<(d[u]-e.c)*e.r)
            {
                d[e.to]=(d[u]-e.c)*e.r;
                if(!inq[e.to])
                {
                    Q.push(e.to);
                    inq[e.to]=1;
                    if(++cnt[e.to]>n)
                        return 0;
                }
            }
        }
    }
    return 1;
}

int main()
{
    int m,no;
    int x=0;
    double sum;
    cin>>n>>m>>no>>sum;
    while(m--)
    {
        int no1,no2;
        double rab,cab,rba,cba;
        scanf("%d%d%lf%lf%lf%lf",&no1,&no2,&rab,&cab,&rba,&cba);
        edges.push_back(Edge(no1,no2,rab,cab));
        G[no1].push_back(x);
        x++;
        edges.push_back(Edge(no2,no1,rba,cba));
        G[no2].push_back(x);
        x++;
    }
    bool flag = bellman_fold(no,sum);
    if(flag)
        cout<<"NO"<<endl;
    else
        cout<<"YES"<<endl;
}

 

poj1860(Bellman—fold)

标签:res   nbsp   may   order   ring   http   问题   file   class   

原文地址:https://www.cnblogs.com/cryingrain/p/8677973.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!