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
题目大意
Nick 起初拥有货币S,他可以交换成其他的货币,交换的过程中存在交换律和手续费,交换后的货币价值=(S-手续费)*交换律,要求判断Nick 通过交换货币可不可以增加自己的资金,并且满足最后的货币种类依旧为S。输入数据:第一行 N-货币种类总数、M-货币可交换方式的个数、S-Nick拥有的货币种类、V-Nick拥有的货币价值,接下来有M行,每行有数据 A,B,R(AB),C(AB),R(BA),C(BA),即可以进行交换的货币种类以及其发生交换对应的交换律和手续费。
解题思路
本题是由货币 S 进行交换,再次返回到 S,判断资金数是否增大。即可转换为判断是否存在正环,所以可以通过改进Bellman-Ford算法进行求解(原始Bellman-Ford 用来判断负环)。
每一种货币标识着图上的一个点,若 A 货币的价值为 V, A->B 的权值为 (V-C(AB))*R(AB), 初始化时w[S]=V,而源点至其他点的权值(即距离)初始化为0。该算法是做n次所有边的松弛操作,当源点价值增加时,直接退出算法。
因为A->B和B->A的转换率和手续费不等,所以构造为有向图。
代码实现
#include <iostream>
#include<cstdio>
using namespace std;
#define maxn 105
double w[maxn],c[maxn][maxn],r[maxn][maxn],money;
int m,n,num,u[maxn*4],v[maxn*4];
bool bellman_ford()
{
while(w[num]<=money)
{
bool update=false;
for(int i=0;i<m*2;i++)
{
int x=u[i],y=v[i];
if((w[x]-c[x][y])*r[x][y]-w[y]>0.0000001)
{
w[y]=(w[x]-c[x][y])*r[x][y];
update=true;
}
}
if(!update) return false;
}
return true;
}
int main()
{
int a,b,i,j;
cin>>n>>m>>num>>money;
for(i=0;i<n;i++)
w[i]=0;
w[num]=money;
for(i=0;i<m*2;i+=2)
{
cin>>a>>b;
u[i]=a,v[i]=b;
u[i+1]=b,v[i+1]=a;
cin>>r[a][b]>>c[a][b]>>r[b][a]>>c[b][a];
}
if(bellman_ford())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}