1. 程式人生 > >Currency Exchange (spfa)

Currency Exchange (spfa)

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

大致題意:有多組資料,每組資料首先輸入n,m,s,v。n表示有n個貨幣兌換點,m表示有m組兌換關係,兌點A到兌點B的匯率和手續費可能與兌點B到兌點A的匯率和手續費不同,是雙向的。s表示源點,v表示初始金額。接下來的m行每行輸入a,b,rab,cad,rba,cba ,表示兌換點a和兌換點b之間的匯率和手續費。問源點s經過一系列的兌換貨幣後回到源點s時的貨幣是否能增加,如果能則輸出yes,否則輸出no。

思路:初始化dis(S)=V 而源點到其他點的距離(權值)初始化為無窮小(0),當s到其他某點的距離能不斷變大時,說明存在最大路徑;如果可以一直變大,說明存在正環。判斷是否存在環路,用Bellman-Ford和spfa都可以。 a點到b點的權值為(v’-cab)*rab b點到a點的權值為(v”-cba)*rba

下面是spfa:

#include<algorithm>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<math.h>
using namespace std;
const int N=105;
const int inf=0x3f3f3f;
struct st
{
    int b;
    double r,c;
    st(int b,double r,double c):b(b),r(r),c(c){}
};
vector<st>g[N];
double v[N];
int spfa(int k,double m)
{
    queue<int>Q;
    Q.push(k);
    while(Q.size())
    {
        int s=Q.front();
        Q.pop();
        int len=g[s].size();
        for(int i=0; i<len; i++)
        {
            st q=g[s][i];
            double p=(v[s]-q.c)*q.r;
            if(p>v[q.b])
            {
                v[q.b]=p;
                Q.push(q.b);
            }
        }
        if(v[k]>m)
            return 1;
    }
    return 0;
}
int main()
{
    int N,M,s;
    double m;
    while(scanf("%d%d%d%lf",&N,&M,&s,&m)!=EOF)
    {
        for(int i=1; i<=N; i++)
        {
            v[i]=0;
            g[i].clear();
        }
        v[s]=m;
        int a,b;
        double rab,cab,rba,cba;
        for(int i=0; i<M; i++)
        {
            scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
            g[a].push_back(st(b,rab,cab));
            g[b].push_back(st(a,rba,cba));
        }
        int ans=spfa(s,m);
        if(ans==1)
            printf("YES\n");
        else
         printf("NO\n");
    }
    return 0;
}