poj 1860 Currency Exchange(SPFA)


题目链接: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 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

Source

Northeastern Europe 2001, Northern Subregion

Description 
一些货币兑换点正在我们的城市工作。我们假设每个兑换点尤其擅长兑换两种特定的货币,并且只进行对这些货币的兑换。可能有一些兑换点专门兑换相同的货币对。每个兑换点都有自己的汇率,货币A到货币B的汇率是你用1货币A换到的货币B的数量。每个兑换点也有一些佣金,即为你需要为你的兑换行动支付的金额。佣金总是从源货币扣除。
例如,如果你想要在汇率为29.75,并且佣金为0.39的兑换点将100美元兑换成俄罗斯卢布,你将会得到(100-0.39)*29.75=2963.3975卢布。
你有N种不同的货币可以在我们的城市进行兑换。我们为每一种货币制定从1到N的唯一一个整数。每个兑换点可以用6个数字形容:整数A和B——它交换的货币(表示为货币A和货币B);实数RAB,CAB,RBA和CBA——当它分别将货币A兑换成货币B和将货币B兑换成货币A时的汇率和佣金。
叫兽有一定数量的货币S,并且它希望它能以某种方式在一些兑换行动后增加它的资本。最后它的资金必须兑换为货币S。
帮助它解决这个棘手的问题。叫兽在进行它的兑换行动时资金总数必须始终非负。
Input 
输入的第一行包含四个数字:N – 货币的数量,M – 兑换点的数量,S – 叫兽具有的货币种类,V – 叫兽具有的货币数量。
下面的M行每行包括6个数字 – 对相应的兑换点的描述。数字相隔一个或多个空格。1<=S<=N<=100,1<=M<=100,V是实数,0<=V<=10^3。
对于每个兑换点汇率和佣金都是实数,小数点后最多有两位小数。10^-2<=汇率<=10^2,0<=佣金<=10^2。
如果在一组兑换行动中没有兑换点被超过一次地使用,我们认为这组兑换行动是简单的。你可以认为最终数值和最初任何一个简单的兑换行动组的比例小于10^4。
Output 
如果叫兽能增加它的财产,输出YES,否则输出NO。
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

思路:我们只需要寻找一下是否有正环存在就可以了!


代码如下:

#include <cstdio>  
#include <cmath>  
#include <cstring>  
#include <cstdlib>  
#include <climits>  
#include <ctype.h>  
#include <queue>  
#include <stack>  
#include <vector>  
#include <deque>  
#include <set>  
#include <map>  
#include <iostream>  
#include <algorithm>  
using namespace std;  
#define pi acos(-1.0)  
#define INF 0xffffff  
#define N 3005  
#define M 2005  
int Edgehead[N];  
int cont[N];  
double dis[N];  
struct  
{  
    int v,next;  
    double rate, comm, w;  
}Edge[2*M];  
bool vis[N];  
int k;  
int n, m, s;  
double val;  
void Addedge(int u,int v,double r, double c)  
{  
    Edge[k].next = Edgehead[u];  
    Edge[k].rate = r;  
    Edge[k].v = v;  
    Edge[k].comm = c;  
    Edge[k].w = 0;  
    Edgehead[u] = k++;  
}  
void init()  
{  
    for(int i = 0 ; i < n ; i++ )//求最长路径开始设为0  
        dis[i] = 0;  
    memset(Edgehead,-1,sizeof(Edgehead));  
    memset(vis,false,sizeof(vis));  
    memset(cont,0,sizeof(cont));  
}  
int SPFA( int start)  
{  
    queue<int>Q;  
    dis[start] = val;  
    vis[start] = true;  
    ++cont[start];  
    Q.push(start);  
    while(!Q.empty())//直到队列为空  
    {  
        int u = Q.front();  
        Q.pop();  
        vis[u] = false;  
        for(int i = Edgehead[u] ; i!=-1 ; i = Edge[i].next)//注意  
        {  
            int v = Edge[i].v;  
            Edge[i].w = (dis[u] - Edge[i].comm)*Edge[i].rate-dis[u];  
            if(dis[v] < dis[u] + Edge[i].w)  
            {  
                dis[v] = dis[u]+Edge[i].w;  
                if( !vis[v] )//防止出现环,也就是进队列重复了    
                {  
                    Q.push(v);  
                    vis[v] = true;  
                }  
                if(++cont[v] > n)//有负环  
                    return -1;  
            }  
        }  
    }  
    return 1;  
}  
int main()  
{  
    while(~scanf("%d%d%d%lf",&n,&m,&s,&val))//n为目的地  
    {  
        k = 0;  
        init();  
        for(int i = 1 ; i <= m ; i++ )  
        {  
            int a, b;  
            double r, c;  
            scanf("%d%d",&a,&b);  
            scanf("%lf%lf",&r,&c);  
            Addedge(a, b, r, c);//双向链表  
            scanf("%lf%lf",&r,&c);  
            Addedge(b, a, r, c);//双向链表  
        }  
        if(SPFA(s) == -1)  
        {  
            printf("YES\n");  
        }  
        else  
            printf("NO\n");  
    }  
    return 0;  
}  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值