poj 1860(SPFA 判环)

Currency Exchange

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 35854   Accepted: 13743

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

题意: 你有某种货币 v,然后题目给你一些 钱币之间的转化, 含汇率 以及 佣金,问你最后能否通过一系列的钱币兑换,增加自己手中的钱。     例如,如果您想在兑换点兑换100美元兑换俄罗斯卢布,汇率为29.75,佣金为0.39,您将获得(100 - 0.39)* 29.75 = 2963.3975RUR。

思路: SPFA判环 ,  在SPFA中记录每个点的入队次数 , 如果有某个点入队次数 >= n ,则含环

AC代码:

///因为有汇率 及 佣金, 用结构体 + 邻接表
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int maxn = 1100;
struct node{
    int r,next;
    double rate,com;
    node(){}
    node(int r,int next,double rate,double com):r(r),next(next),rate(rate),com(com){}
}edge[maxn*2];
double dis[maxn],v;
int vis[maxn];
int cnt[maxn];
int head[maxn];
int n,m,s;

bool SPFA(int st,double have){
    memset(dis,0,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(cnt,0,sizeof(cnt));
    dis[st] = have;
    vis[st] = 1;
    cnt[st] ++;
    queue<int>Q;
    Q.push(st);
    while(!Q.empty()){
        int now = Q.front();
        Q.pop(); vis[now] = 0;
        for(int i = head[now];i != -1;i = edge[i].next){
            int v = edge[i].r;
            double r = edge[i].rate, c = edge[i].com;
          //  printf("%.3lf %.3lf %.3lf %.3lf %.3lf\n",dis[v],dis[now],c,r,(dis[now] - c) * r);
            if(dis[v] < (dis[now] - c) * r){
                dis[v] = (dis[now] - c) * r;
                if(!vis[v]){
                    Q.push(v);
                    vis[v] = 1;
                    cnt[v] ++;
                    if(cnt[v] >= n) return 1;
                }
            }
        }
    }
    return 0;
}

int main()
{
    while(~scanf("%d%d%d%lf",&n,&m,&s,&v)){
        memset(head,-1,sizeof(head));
        int pe = 0,A,B;
        double r,c,r1,c1;
        for(int i = 0;i < m;i ++){
            scanf("%d%d%lf%lf%lf%lf",&A,&B,&r,&c,&r1,&c1);
            edge[pe] = node(B,head[A],r,c);
            head[A] = pe ++;
            edge[pe] = node(A,head[B],r1,c1);
            head[B] = pe ++;
        }
        if(SPFA(s,v)) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/no_O_ac/article/details/81279372