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- 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 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 point exchange rates and commissions is real, given with the most of the 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 Sequen Ce. 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.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00
Sample Output
YES
Some currencies with a value of s are exchanged through a series of exchanges, whether they can add value. Abstract is a currency represents a node, the weight between the nodes is the value of the exchange rules, because the last is to seek the original currency, therefore is a loop, the currency through this loop back appreciation, indicating that the loop is positive.
Using the Bellman-ford algorithm, the original is to determine whether there is a negative loop and find the shortest, here as long as a slight change in the relaxation operation can be judged whether there is a positive loop
#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <cmath>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN#define MOD 1000000007using namespace STD;intN,m,s,e;DoubleV,DIS[MAXN];structnode{intBeg,end;DoubleR,c;}; Node edge[maxn<<1];voidAddintBintEnDoubleRDoublec) {edge[e].beg=b; edge[e].end=en; Edge[e].r=r; Edge[e].c=c; e++;}BOOLRelaxintP) {Doublet= (DIS[EDGE[P].BEG]-EDGE[P].C) *EDGE[P].R;if(dis[edge[p].end]<t) {dis[edge[p].end]=t;return true; }return false;}BOOLBellman () {memset(Dis,0,sizeof(dis)); Dis[s]=v; for(intI=1; i<n; ++i) {BOOLflag=false; for(intj=0; j<e; ++J) {if(Relax (j)) flag=true; }if(DIS[S]>V)return true;if(!flag)return false; } for(intj=0; j<e; ++J)if(Relax (j))return true;return false;}intMain () {e=0;intb;DoubleRAB,CAB,RBA,CBA;scanf("%D%D%D%LF", &n,&m,&s,&v); for(intI=0; i<m; ++i) {scanf("%D%D%LF%LF%LF%LF", &A,&B,&RAB,&CAB,&RBA,&CBA); Add (A,b,rab,cab); Add (B,A,RBA,CBA); }if(Bellman ())printf("yes\n");Else printf("no\n");return 0;}
Poj1860--currency Exchange (eellman-ford+-weighted loop)