【SPFA判断正负环】[kuangbin带你飞]专题四 最短路 POJ - 1860 E - Currency Exchange

E - Currency Exchange

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 R AB, C AB, R BA and C BA - 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<=10 3. 
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10 -2<=rate<=10 2, 0<=commission<=10 2. 
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 10 4. 

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
//SPFA是bellman的优化,可以解决此题,其实此题就是想知道能不能组成正环,如果有一条边为负的话
//此点松弛n次之后,还会继续改变值
//SPFA需要设队列,把每次需要优化的点放进去,如果它所能到的点,经过此次优化可以得到更优,就将他们放入队列
//重点在于,每次从队列中拿出点,vis[]变回0,,这样之后可以再放进去,放进队列时,vis[],标记为1

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int maxn=1005;
int n,m,sum,vis[maxn],cnt[maxn];
double val,dis[maxn];

struct node
{
    int v;
    double r,c;
};
vector <node> vec[maxn];

int SPFA()
{
    queue <int> q;
    dis[sum]=val;
    q.push(sum);
    while(!q.empty())
    {
        int v;
        double r,c;
        int u=q.front();
        q.pop();
        vis[u]=0;//!!!!!
        for(int i=0;i<vec[u].size();i++)
        {
            v=vec[u][i].v;
            r=vec[u][i].r;
            c=vec[u][i].c;
            if(dis[v]<(dis[u]-c)*r)
            {
                dis[v]=(dis[u]-c)*r;
                if(!vis[v])
                {
                    cnt[v]++;
                    if(cnt[v]>n) return 1;
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    return -1;
}

int main()
{
    memset(vis,0,sizeof(vis));
    memset(dis,0,sizeof(dis));
    memset(cnt,0,sizeof(cnt));
    scanf("%d%d%d%lf",&n,&m,&sum,&val);
    for(int i=1;i<=m;i++)
    {
        int u,v;
        double Rab,Rba,Cab,Cba;
        scanf("%d%d%lf%lf%lf%lf",&u,&v,&Rab,&Cab,&Rba,&Cba);
        node e;
        e.v=v,e.r=Rab,e.c=Cab;
        vec[u].push_back(e);
        e.v=u,e.r=Rba,e.c=Cba;
        vec[v].push_back(e);
    }
    int ans=SPFA();
    if(ans==1) printf("YES\n");
    else printf("NO\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41037114/article/details/81111638
今日推荐