Currency Exchange-poj 1860

Source: Internet
Author: User

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 22111 Accepted: 7986

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

Sample Output

YES
This problem uses the Bellman-ford algorithm, the judgment condition can be modified to find whether the right to increase the loop
1#include <iostream>2#include <string.h>3 using namespacestd;4 structExch {5     intsrc, des;6     floatRate , commission;7} ex[202];8 intMain () {9     intcur_num, Exp_num, cur;Ten     floatInit; One     floatc[101]; A      while(Cin >> cur_num >> exp_num >> cur >>init) { -Memset (c,0,101*sizeof(float)); -C[cur] =Init; the          for(inti =0; i < Exp_num; i++) { -             intA, B; -Cin >> a >>b; -ex[2* I].SRC =A; +ex[2* I].des =b; -CIN >> ex[2* I].rate >> ex[2*i].commission; +ex[2* i +1].SRC =b; Aex[2* i +1].des =A; atCIN >> ex[2* i +1].rate >> ex[2* i +1].commission; -         } -          for(inti =0; i < Cur_num; i++) { -              for(intj =0; J <2* EXP_NUM; J + +) { -                 if(C[ex[j].des] -< (C[EX[J].SRC]-ex[j].commission) *ex[j].rate) { inC[ex[j].des] = (C[EX[J].SRC]-ex[j].commission) -*ex[j].rate; to                 } +             } -         } the         intFlag =0; *          for(intj =0; J <2* EXP_NUM; J + +) { $             if(C[ex[j].des] < (C[EX[J].SRC]-ex[j].commission) *ex[j].rate) {Panax Notoginsengflag=1; -                  Break; the             } +         } A         if(flag==1){ thecout<<"YES"<<Endl; +}Else{ -cout<<"NO"<<Endl; $         } $  -     } -     return 0; the  -}

Currency Exchange-poj 1860

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.