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
The main topic: There are a variety of remittance, exchange between the currency can be exchanged, which requires a fee, when you use 100A currency exchange B currency, A to B exchange rate is 29.75, the handling fee is 0.39, then you can get (100-0.39) * 29.75 = 2963.3975 b currency. Ask if the amount of s currency can be increased by exchanging the amount of s currency that is eventually obtained.
The exchange of money can be repeated several times, so we need to find out whether there is a positive loop, and the resulting s amount is increased.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5 using namespacestd;6 Const intmaxn= -;7 structnode8 {9 intA//Currency aTen intb//Currency b One DoubleRate ; A Doublecom//Handling Fee - }PATH[MAXN]; - intN//Number of Currencies the intM//number of redemption points - intS//The first Currencies - DoubleV//holding the principal of the S currency - intindex; + DoubleVAL[MAXN];//s to each side of the weight value - BOOLBellman () + { A BOOLFlag; atMemset (Val,0,sizeof(Val)); -val[s]=v; - for(intI=1; i<=n;i++) - { -flag=false; - for(intj=1; j<index;j++) in { - if(val[path[j].b]< (val[path[j].a]-path[j].com) *path[j].rate) to { +val[path[j].b]= (val[path[j].a]-path[j].com) *path[j].rate; -flag=true; the } * } $ if(!flag)Panax Notoginseng Break; - } the returnFlag; + } A intMain () the { + intb; - Doublea1,a2,b1,b2; $scanf"%D%D%D%LF",&n,&m,&s,&v); $index=1; - for(intI=1; i<=m;i++) - { thescanf"%D%D%LF%LF%LF%LF",&a,&b,&a1,&b1,&a2,&B2); -path[index].a=a,path[index].b=b;WuyiPath[index].rate=a1,path[index].com=B1; theindex++; -path[index].a=a,path[index].b=b; WuPath[index].rate=a2,path[index].com=B2; -index++; About } $ if(Bellman ()) -printf"yes\n"); - Else -printf"no\n"); A return 0; +}
View Code
POJ 1860 Currency Exchange