http://poj.org/problem?id=1860
Currency Exchange
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 22461 |
|
Accepted: 8115 |
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 number of currencies, m-the number of exchange points, s-th E Number of currency Nick has and v-the quantity of the currency units 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 of the point exchange rates and commissions is real, given with in the most of the digits after the decimal point, 10-2<=ra te<=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. 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 be 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.001 2 1.00 1.00 1.00 1.002 3 1.10 1.00 1.10 1.00
Sample Output
YES
The main idea: Exchange currency, a total of n currency, m type of exchange, Nick has the currency of the type S, the value of the currency is W, the rate of exchange of currency A to B is the R fee of C, asked to return the currency of the type S when the nick can benefit
Data analysis:
3 2 1 20.00//above 1 2 1.00 1.00 1.00 1.002 3 1.10 1.00 1.10 1.00//Currency 2 for currency 3 The run rate is 1.10, currency 1.00 is 3, the rate of exchange for currency 2 is 1.1
#include <stdio.h>#include<string.h>#include<math.h>#include<vector>#include<queue>#include<stdlib.h>#include<ctype.h>#defineINF 0x3f3f3f3f#defineMax (A, B) (a > B? a:b)#defineMin (A, B) (a < b? a:b)#defineN 110using namespacestd;structnode{inte; DoubleR, c;}; Vector<node>G[n];intN;BOOLVis[n];DoubleW, Dist[n];voidInit () {inti; memset (Vis,false,sizeof(VIS)); for(i =1; I <= N; i++) {g[i].clear (); Dist[i]=0; }}intSPFA (ints) {Queue<node>Q; node p, PN; intI, Len; P.E=s; Dist[s]=W; Q.push (P); Vis[s]=true; while(!Q.empty ()) {P=Q.front (); Q.pop (); VIS[P.E]=false; Len=g[p.e].size (); for(i =0; i < Len; i++) {pn=G[p.e][i]; if(DIST[PN.E] < (DIST[P.E]-pn.c) *PN.R) {DIST[PN.E]= (DIST[P.E]-pn.c) *PN.R; if(!VIS[PN.E]) {Q.push (PN); VIS[PN.E]=true; } if(Dist[s] > w && pn.e = =s)return 1; } } } return-1;}intMain () {node p; intm, S, I, A, b; DoubleR, C; while(SCANF ("%D%D%D%LF", &n, &m, &s, &w)! =EOF) {Init (); for(i =1; I <= m; i++) {scanf ("%d%d", &a, &b); scanf ("%LF%LF", &r, &c); P.E= B, P.R = r, p.c =C; G[a].push_back (P); scanf ("%LF%LF", &r, &c); P.E= A, P.R = r, p.c =C; G[b].push_back (P); } if(SPFA (s) = =1) printf ("yes\n"); Elseprintf ("no\n"); } return 0;}
0, Handling fee is 1.00
POJ 1860 Currency Exchange