Currency Exchange
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
|
题意:现有N种纸币,M家兑换点,每家兑换点只能进行两种纸币的互换,即,a换成b或者b换成a。现在,有S型的纸币V元,问你是否可以通过不断的兑换来使钱增多。接下来的M行,每行6个数值,a,b,表示该家兑换点可以兑换的钱的类型。Rab,Cab,Rba,Cba。分别表示a换成b和b换成a的兑换率和收取的手续费用。
思路:只需判断是否出现一个正环即可,如果出现正环,那么一定可以通过不断的进行正环而使钱增多,所以我们可以用spfa来判环!
#include "iostream"
#include "vector"
#include "queue"
using namespace std;
int N,M,S;
double V;
struct edge//R表示利率,C表示消耗
{
int to;
double R,C;
};
vector<edge> G[105];
int vis[105],cnt[105];//vis记录是否在队列中,cnt记录每个点入队的次数,一个点最对入队N次,如果超过N,说明有环!
double d[105];//记录有多少钱
bool spfa()
{
queue<int> que;
que.push(S);
vis[S]=1;
d[S]=V;
while(!que.empty()){
int u=que.front();
que.pop();
vis[u]=0;
for(int i=0;i<G[u].size();i++){
edge e=G[u][i];
double val=(d[u]-e.C)*e.R;//兑换成另一种纸币后的钱
if(val-d[e.to]>1e-8){//由于是double,避免出现精度误差
d[e.to]=val;
if(vis[e.to]) continue;
que.push(e.to);
vis[e.to]=1;
if(++cnt[e.to]>N) return 1;
}
}
}
return 0;
}
int main()
{
ios::sync_with_stdio(false);
cin>>N>>M>>S>>V;
int a,b;
double Rab,Cab,Rba,Cba;//分别表示a换成b的利率和消耗,b换成a的利率和消耗
for(int i=0;i<105;i++) G[i].clear();
for(int i=0;i<M;i++){
cin>>a>>b>>Rab>>Cab>>Rba>>Cba;
edge e;
e.to=b,e.R=Rab,e.C=Cab;
G[a].push_back(e);
e.to=a,e.R=Rba,e.C=Cba;
G[b].push_back(e);
}
if(spfa())//如果出现正环,即可以使钱变多!
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}