Poj 1860 currency exchange (spfa)

Source: Internet
Author: User


Question link: http://poj.org/problem? Id = 1860


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.01 2 1.00 1.00 1.00 1.002 3 1.10 1.00 1.10 1.00

Sample output

YES

Source

Northeastern Europe 2001, northern subregion
Description some currency exchange points are working in our city. We assume that each exchange point is particularly good at exchanging two specific currencies and only exchanges these currencies. Some exchange points may be used to redeem the same currency pair. Each exchange point has its own exchange rate. The exchange rate from currency A to currency B is the number of currency B you change from currency A to currency B. Each exchange point also has some Commission, that is, the amount you need to pay for your exchange operation. The Commission is always deducted from the source currency. For example, if you want to exchange $29.75 into a Russian rubles at a rate of 0.39 and a commission of 100, you will get (100-0.39) * 29.75 = 2963.3975 rubles. You have n different currencies that can be exchanged in our city. We define a unique integer from 1 to n for each currency. Each exchange point can be described in six numbers: integers A and B-the currency it exchanges (expressed as currency A and currency B); real numbers Rab, cab, RBA and CBA-the exchange rate and Commission when it exchanges currency A into currency B and currency B into currency A respectively. The beast has a certain number of currencies, and it wants it to increase its capital in some way after some exchanges. Finally, its funds must be exchanged into the currency S. Help it solve this thorny problem. The total amount of funds required for an animal to redeem it must always be non-negative. The first line of input contains four numbers: N-currency quantity, M-exchange point quantity, s-the currency type of the animal, and V-the number of currencies of the animal. Each row in the M line below contains six numbers-A description of the corresponding exchange point. A number is separated by one or more spaces. 1 <= S <= n <= 100, 1 <= m <=, V is a real number, 0 <= V <= 10 ^ 3. For each exchange point, the exchange rate and Commission are real numbers, with a maximum of two decimal places. 10 ^-2 <= exchange rate <= 10 ^ 2, 0 <= Commission <= 10 ^ 2. If no exchange point is used more than once in a group of exchange operations, we think this group of exchange operations is simple. You can think that the ratio of the final value to any initial simple exchange action group is less than 10 ^ 4. Output: If a beast can increase its property, output yes; otherwise, output No. Sample input 3 2 1 20.01 2 1.00 1.00 1.00 3 1.002 1.10 1.00 sample output Yes

Idea: we only need to find out if a positive ring exists!


The Code is as follows:

# Include <cstdio> # include <cmath> # include <cstring> # include <cstdlib> # include <climits> # include <ctype. h> # include <queue> # include <stack> # include <vector> # include <deque> # include <set> # include <map> # include <iostream> # include <algorithm> using namespace STD; # define PI ACOs (-1.0) # define INF 0 xffffff # define n 3005 # define M 2005 int edgehead [N]; int cont [N]; double dis [N]; struct {in T v, next; double rate, comm, W;} edge [2 * m]; bool vis [N]; int K; int n, m, s; double val; void addedge (int u, int V, Double R, double C) {edge [K]. next = edgehead [u]; edge [K]. rate = r; edge [K]. V = V; edge [K]. comm = C; edge [K]. W = 0; edgehead [u] = K ++;} void Init () {for (INT I = 0; I <n; I ++) // set the longest path to 0 dis [I] = 0; memset (edgehead,-1, sizeof (edgehead); memset (VIS, false, sizeof (VIS); memset (cont, 0, sizeof (cont);} int spfa (INT start) {queue <int> q; DIS [start] = val; vis [start] = true; ++ cont [start]; q. push (start); While (! Q. empty () // until the queue is empty {int u = Q. front (); q. pop (); vis [u] = false; For (INT I = edgehead [u]; I! =-1; I = edge [I]. next) // note {int v = edge [I]. v; edge [I]. W = (DIS [u]-edge [I]. comm) * edge [I]. rate-Dis [u]; If (DIS [v] <dis [u] + edge [I]. w) {dis [v] = dis [u] + edge [I]. w; If (! Vis [v]) // prevents loops, that is, duplicate packets in the queue {q. push (V); vis [v] = true;} If (++ cont [v]> N) // return-1;} return 1 ;} int main () {While (~ Scanf ("% d % lf", & N, & M, & S, & Val) // n is the destination {k = 0; Init (); for (INT I = 1; I <= m; I ++) {int A, B; Double R, C; scanf ("% d", &, & B); scanf ("% lf", & R, & C); addedge (a, B, R, C ); // scanf ("% lf", & R, & C); addedge (B, A, R, C ); // bidirectional linked list} If (spfa (S) =-1) {printf ("Yes \ n");} else printf ("NO \ n ");} 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.