Currency Exchange
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.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00
Sample Output
YES
Main topic:
The principal of a given currency. The exchange rate and handling fee for the conversion between some currencies is given. Determines whether the principal can be changed by a conversion between currencies.
Problem Solving Ideas:
Creates a map based on the given currency conversion.
The Bellman-ford algorithm is used to N-1 the secondary relaxation of the graph. (n is the number of vertices)
If a figure does not exist in the positive loop, then the maximum number of N-1 relaxation, if there is a positive weight loop, you can then relax.
Code:
1 /*************************************************************************2 > File Name:poj1860.cpp3 > Author:enumz4 > Mail: [email protected]5 > Created time:2014 October 17 Friday 17:08 07 seconds6 ************************************************************************/7 8#include <iostream>9#include <cstdio>Ten#include <cstdlib> One#include <string> A#include <cstring> -#include <list> -#include <queue> the#include <stack> -#include <map> -#include <Set> -#include <algorithm> + #defineMAXN 2000 - using namespacestd; + intn,m,s,k; A DoubleDIS[MAXN]; at DoubleV; - structEdge - { - intBegin,end; - Doubler,c; - }EDGE[MAXN]; in BOOLBellman () - { tomemset (DIS,0,sizeof(DIS)); +dis[s]=V; - BOOLflag=0; the for(intI=1; i<=n-1; i++) * { $flag=0;Panax Notoginseng for(intj=1; j<=k;j++) - if(dis[edge[j].end]< (DIS[EDGE[J].BEGIN]-EDGE[J].C) *EDGE[J].R) the { +dis[edge[j].end]= (DIS[EDGE[J].BEGIN]-EDGE[J].C) *EDGE[J].R; Aflag=1; the } + if(!flag) Break; - } $ //again to determine whether it will increase, the increase will indicate that there is a positive power loop $ for(intj=1; j<=k;j++) - if(dis[edge[j].end]< (DIS[EDGE[J].BEGIN]-EDGE[J].C) *EDGE[J].R) - return 1; the return 0; - }Wuyi intMain () the { - while(SCANF ("%D%D%D%LF", &n,&m,&s,&v)! =EOF) Wu { -k=1; About for(intI=1; i<=m;i++) $ { - intb; - DoubleRAB,RBA,CAB,CBA; -scanf"%D%D%LF%LF%LF%LF",&a,&b,&rab,&cab,&rba,&CBA); Aedge[k].begin=A; +Edge[k].end=b; theEdge[k].r=rab,edge[k].c=cab; -k++; $edge[k].begin=b; theEdge[k].end=A; theEdge[k].r=rba,edge[k].c=CBA; thek++; the } -k--; in BOOLok=Bellman (); the if(OK) cout<<"YES"<<Endl; the Elsecout<<"NO"<<Endl; About } the return 0; the}
Poj1860--currency Exchange (Bellmanford algorithm to find the shortest path)