POJ1860 Currency Exchange "Bellmanford algorithm" "to seek the right loop"

Source: Internet
Author: User

Currency ExchangeTime limit:1000msMemory limit:30000kTotal submissions:20994accepted:7522
Description

Several Currency exchange points is working. Let us suppose so each of the specializes in the particular currencies and performs exchange operations only with these C Urrencies. There can several points specializing in the same pair of currencies. Each point has its own exchange rates, and exchange rate of A to B is the quantity of B you get for 1A. Also each of the exchange point has some commission, the sum of which has a to pay for your exchange operation. Commission is all collected in source currency.
For example, if you want to exchange from US Dollars into Russian rubles at the exchange point, where the 29.75, and the Commission is 0.39 you'll get (100-0.39) * 29.75 = 2963.3975RUR.
You surely know that there is N different currencies you can deal with in our city. Let us assign the unique integer number from 1 through N to each currency. Then the exchange point can is described with 6 Numbers:integer A and b-numbers of currencies it exchanges, and real R AB, 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 has his money in currency S in the end. Help him to answer this difficult question. Nick must always has non-negative sum of money while making his operations.


Input

the first line of the input contains four numbers:n-the Numbe R of currencies, m-the number of exchange points, s-the number of currency Nick has and v-the quantity of currency u NITs he has. The following M lines contain 6 numbers each-the description of the corresponding Exchange point-in specified above or Der Numbers is 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 comm Issions is real, given with at most of the digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102.& nbsp
Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this Sequen Ce. 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'll is 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


Source

Northeastern Europe 2001, Northern subregion


The main topic: There are n currencies, currencies can be exchanged by exchange rate, but also to receive a handling fee, when you use 100A currency to Exchange B currency,

If the exchange rate of A to B is 29.75 and the handling fee is 0.39, you can get the B currency (100-0.39) *29.75 = 2963.3975. Currency

You can always repeat the exchange, Q: If you can increase the value of your currency by exchanging currency, then output "YES" or Output "NO".

Idea: To think of n currencies as N points on a graph, when you have a quantity of V in a currency,

The weighted value between the currency AB is--(v-fee) *a to B

The problem can be converted to the maximum path in which the graph can be infinitely enlarged (containing a positive loop). So how to judge if there is a right back

Lu. Can be done with the idea of the Bellmanford algorithm.

Bellmanford is to seek the shortest path, and to determine whether there is a negative power loop. Here, use Bellmanford to find the longest path and determine if

There is a positive power loop.


<span style= "Font-family:microsoft Yahei;" > #include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using     namespace Std;const int maxn = 110;const int MAXM = 220;const int INF = 0xffffff0;struct edgenode{int to;   Connected B-point int next; Double rate;//double cost;} Edges[maxm];int HEAD[MAXN];d ouble dist[maxn];//m--redemption point number n--currency species s--holding the first s currency v--holding the first S currency funds bool Bellmanford (int m,int N    , int s,double v) {Dist[s] = V;  for (int i = 1, i < n; ++i) {for (int j = 1, J <= N; ++j) {for (int k = head[j]; K! =-1; K = edges[k].next)//Find the longest path {if (Dist[edges[k].to] < (dist[j]-edges[k].cost) *edges[k].rat            e) dist[edges[k].to] = (dist[j]-edges[k].cost) *edges[k].rate;            }}} for (int j = 1, J <= N; ++j) {for (int k = head[j]; K! =-1; k = edges[k].next) { if (Dist[edges[k].to] < (dist[j]-edges[k].cost) *edges[K].rate)//s to other paths can always increase, indicating that there is the longest path return true; }} return false;}    int main () {int n,m,s,u,v;    Double V,RAB,CAB,RBA,CBA;        while (~SCANF ("%d%d%d%lf", &n,&m,&s,&v)) {int id = 0;        memset (head,-1,sizeof (Head));            memset (dist,0,sizeof (Dist));        The initial is 0, not the INF, because Bellmanford is used to find the negative ring, here to find whether there is a positive ring memset (edges,0,sizeof (Edges)); for (int i = 0; i < M; ++i) {scanf ("%d%d%lf%lf%lf%lf", &u,&v,&rab,&cab,&rba,&c            BA);            Edges[id].to = v;            Edges[id].rate = Rab;            Edges[id].cost = Cab;            Edges[id].next = Head[u];            Head[u] = id++;            edges[id].to = u;            Edges[id].rate = Rba;            Edges[id].cost = Cba;            Edges[id].next = Head[v];        HEAD[V] = id++;        } if (Bellmanford (id,n,s,v)) printf ("yes\n");    else printf ("no\n"); } return 0;} </span>


POJ1860 Currency Exchange "Bellmanford algorithm" "to seek the right loop"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.