Currency Exchange

来源:互联网 发布:windows http server 编辑:程序博客网 时间:2024/05/16 09:30

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和spfa可以判断。


bellman:

#include <iostream>#include <cstdio>#include <cstring>#define mem(a) memset(a,false,sizeof(a));using namespace std;struct Node{int from;int to;double rate;double com;};Node e[220];double d[120];bool bellman(double v,int s,int n,int edge){for(int i = 1;i <= n;i++) //对所有的货币兑换初始化为0 {d[i] = 0;              }d[s] = v;  //d[i]表示初始时刻的s兑换的钱 bool flag;for(int i = 1;i <= n;i++)  //n个点最短路最多有n-1条 {flag = false;for(int j = 0;j < edge;j++){if(d[e[j].to] < (d[e[j].from] - e[j].com) * e[j].rate) //找到能兑换更多的方式,可能包含回路 {flag = true;d[e[j].to] = (d[e[j].from] - e[j].com) * e[j].rate;}}if(!flag){return false;}if(i == n){return true;}}//for(int i = 0;i < edge;i++)//{//if(d[e[i].to] < (d[e[i].from] - e[i].com) * e[i].rate)//{//return true;//}//}}int main(){int n,m,s;double v;while(scanf("%d%d%d%lf",&n,&m,&s,&v) != EOF){int A,B;int k = 0;double rab,cab,rba,cba;for(int i = 0;i < m;i++){scanf("%d%d%lf%lf%lf%lf",&A,&B,&rab,&cab,&rba,&cba);e[k].from = A;e[k].to = B;e[k].rate = rab;e[k++].com = cab;e[k].from = B;e[k].to = A;e[k].rate = rba;e[k++].com = cba;}if(bellman(v,s,n,k)){printf("YES\n");}else{printf("NO\n");}}return 0;}

SPFA:

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define mem(a) memset(a,0,sizeof(a));using namespace std;struct Node{double rate;double com;};bool inQueue[120];  //入队没有 int cnt[120];       //入队次数 Node map[120][120];double d[120];bool spfa(int n,int s,double v){mem(cnt);mem(inQueue);for(int i = 1;i <= n;i++){d[i] = 0;}d[s] = v;queue<int> q;q.push(s);inQueue[s] = true;cnt[s] = 1;while(!q.empty()){int t = q.front();q.pop();inQueue[t] = false; for(int i = 1;i <= n;i++)  {if(map[t][i].rate != 0.0)  //与出队点能够兑换 {if(d[i] < (d[t] - map[t][i].com) * map[t][i].rate) //兑换了钱变多 {d[i] = (d[t] - map[t][i].com) * map[t][i].rate;if(!inQueue[i]){q.push(i);inQueue[i] = true;}if(++cnt[i] == n)  //入队次数达到了n次,说明存在环使得钱变多 (因为每次都更新){return true;}}}}}return false;}int main(){int n,m,s;double v;while(scanf("%d%d%d%lf",&n,&m,&s,&v) != EOF){int A,B;int k = 0;double rab,cab,rba,cba;for(int i = 1;i <= n;i++){for(int j = 1;j <= n;j++){map[i][j].rate = 0;}}for(int i = 0;i < m;i++){scanf("%d%d%lf%lf%lf%lf",&A,&B,&rab,&cab,&rba,&cba);map[A][B].rate = rab;map[A][B].com = cab;map[B][A].rate = rba;map[B][A].com = cba;}if(spfa(n,s,v)){printf("YES\n");}else{printf("NO\n");}}return 0;}


0 0