Graph theory --- spfa + chain forward Star: determines whether a positive loop poj 1860: currency exchange exists

Source: Internet
Author: User
  Currency exchange
Time limit:1000 ms   Memory limit:30000 K
Total submissions:19881   Accepted:7114
 

Description

Several currency exchange points are working in our city. let us suppose that each point specializes in two particle 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.20.rur.
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

 

Source

Northeastern Europe 2001, northern subregion

 

Mean:

 

You have some ancient coins. Now you want to use these coins to redeem them into other coins. There are n exchange points in this city, each of which includes:
A ---- coin
B ---- coin B
RAB -- ratio of A to B
Cab -- A: the service charge for converting A to B
RBA -- ratio of B to
CBA-the Commission for converting B to
Now you have the ancient coins numbered S. You will use these coins for a series of exchanges, and eventually you will need to redeem them back. You want to know if you can increase your currency through a series of exchanges.
N -- total number of coins (points)
M -- number of exchange points (number of sides)
S -- your currency type identifier (start point & End Point)
V -- the number of currencies in your current body

 

 

Analyze:

Determine whether a positive loop exists in the graph.

Use spfa to continuously iterate to find the maximum path. If the number of iterations of a certain point exceeds N in this process, a positive weight loop exists.

In fact, under normal circumstances, the number of iterations for each point will not exceed 2, so this question can be changed to 3. Of course, if there is a positive loop, it will definitely exceed n, therefore, n is used to determine insurance when no time is overdue.

 

Time Complexity:O (M * K), k is the average number of iterations for each point

 

Source code:

 

// Memory time // 164 K 0 Ms //: snarl_jsb # include <algorithm> # include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <Stack> # include <iomanip> # include <string> # include <climits> # include <cmath> # define maxv 110 # define MaxE 110 <1 # define ll long longusing namespace STD; int n, m, Sta; float num; int vis [maxv]; float dis [maxv]; int CNT [maxv]; namespace adj {struct Edge {int to, next; float rate, cost;}; edge [MaxE]; int top; int head [maxv]; void Init () {Top = 1; memset (Head, 0, sizeof (head);} void addedge (int u, int V, float rate, float cost) {edge [Top]. to = V; edge [Top]. rate = rate; edge [Top]. cost = cost; edge [Top]. next = head [u]; head [u] = Top ++ ;}} using namespace adj; bool spfa () {for (INT I = 1; I <= N; I ++) CNT [I] = vis [I] = 0, DIS [I] = 0.0; queue <int> q; q. push (STA); vis [sta] = 1; DIS [sta] = num; while (! Q. empty () {int now = Q. front (); q. pop (); vis [now] = 0; For (INT I = head [now]; I; I = edge [I]. next) {int son = edge [I]. to; float TMP = (DIS [now]-edge [I]. cost) * edge [I]. rate * 1.0; If (DIS [son] <TMP) {dis [son] = TMP; If (! Vis [son]) {q. push (son); vis [son] = 1;} CNT [son] ++; If (CNT [son]> 3) // a node has more than N iterations, positive loop return false; }}return true;} int main () {// freopen ("cin.txt", "r", stdin ); // freopen ("cout.txt", "W", stdout); scanf ("% d % F", & N, & M, & STA, & num); adj: Init (); int A, B; float R1, C1, R2, C2; while (M --) {scanf ("% d % F", & A, & B, & R1, & C1, & R2, & C2); adj :: addedge (a, B, R1, C1); adj: addedge (B, A, R2, C2);} If (! Spfa () puts ("yes"); else puts ("no"); Return 0 ;}

 

  

 

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.