Currency Exchange

Time Limit: 1000MS

 

Memory Limit: 30000K

Total Submissions: 27626

 

Accepted: 10255


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


题意:货币兑换问题,a种货币兑换b种货币需要进行的运算是(兑换金币 - 手续费)*汇率。

输入第一行n,m,s,v。n是货币的种类,m是几种货币的兑换,s是现有的那种货币,v是有s货币的数量。然后第m行数据,每一行有六个数据a,b,a兑换b的汇率,a兑换b的手续费,b兑换a的汇率,b兑换a的手续费。

题解:用到Bellmen算法,Bellmen既可以求负权值环,同理也可以求正权值环,求负权值是不断的缩小每一条路,求正权值是不断的放大每一条路,所以这个题就变成了最长路。与一个题(​​POJ 3259Wormholes​​)形成对比。

注意一些用bouble的地方就行了,还有数组内存开多大的问题(双向*2)。


#include <iostream>
#include <string.h>
using namespace std;

int n, s;
double v;

struct node
{
int a, b;
double rate, com;
} num[225];

double div[225];

int Bellmen(int m)
{
int i, j;
for(i = 0; i <= m; i++)
{
div[i] = 0;
}
div[s] = v;
int check;
for(i = 1; i < n; i++)
{
check = 0;
for(j = 1; j <= m; j++)
{
if(div[num[j].b] < (div[num[j].a] - num[j].com )* num[j].rate)
{
check = 1;
div[num[j].b] = (div[num[j].a] - num[j].com )* num[j].rate;
}
}
if(check == 0)
break;
}
for(j = 1; j <= m; j++)
{
if(div[num[j].b] < (div[num[j].a ] - num[j].com )* num[j].rate)
{
return 1;
}
}
return 0;
}

int main()
{
int m, i, j, x, y;
double r1, r2, c1, c2;
while(cin>>n>>m>>s>>v)
{
for(j = 0, i = 1; i <= m; i++)
{
cin>>x>>y>>r1>>c1>>r2>>c2;
j++;
num[j].a = x;
num[j].b = y;
num[j].rate = r1;
num[j].com = c1;
j++;
num[j].a = y;
num[j].b = x;
num[j].rate = r2;
num[j].com = c2;
}
if(Bellmen(j))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}