判断负环poj1860

来源:互联网 发布:手机淘宝怎么开不了店 编辑:程序博客网 时间:2024/05/16 17:32

Currency Exchange
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 28532 Accepted: 10651

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.01 2 1.00 1.00 1.00 1.002 3 1.10 1.00 1.10 1.00

Sample Output

YES

题意:大致意思是有很多地方,现在每个地方有特定的两种金币交换,然后有一个人要在这个地方走,问是否能有一种方法使得交换后金钱增加。

题解:其实就是一道判断是否存在负环的题目,如果存在负环,其实就是如果存在一个环走过之后金钱增加,则不断走这条路,结果金钱肯定可以做到无限大,然后再回到原点,肯定也是能增加的。所以只需判断是否存在负环就可以了,用的是bellman-ford算法,还有一个spfa的算法,这个不会,下次去看看。

贴下代码:

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <queue>#include <sstream>#include <fstream>#include <set>#include <map>#define INF 1e9using namespace std;int n,m,s;double v;struct atlas{    int fr,to;    double k1,c1;    double k2,c2;}ar[150];//这个就是边,即图double d[150];bool short_path(int s,double mon){    memset(d,0,sizeof(d));    d[s] = mon;//初始化    for (int i = 0;i < n;i++)    {        for (int j = 0;j < m;j++)        {            atlas e = ar[j];            if(d[e.to] < (d[e.fr] - e.c1)*e.k1)            {                d[e.to] = (d[e.fr] - e.c1)*e.k1;                if(i == n-1) return true;//因为如果不存在负环,则最多通过n-1条边            }            if(d[e.fr] < (d[e.to] - e.c2)*e.k2)            {                d[e.fr] = (d[e.to] - e.c2)*e.k2;                if(i == n-1) return true;//同上            }        }    }    return false;}int main(){    while (cin>>n)    {        cin>>m>>s>>v;        for (int i = 0;i < m;i++)        {            cin>>ar[i].fr>>ar[i].to>>ar[i].k1>>ar[i].c1>>ar[i].k2>>ar[i].c2;        }        if(short_path(s,v)) cout<<"YES"<<endl;        else cout<<"NO"<<endl;    }    return 0;}

0 0