Bellman_ford algorithm Currency Exchange POJ1860

Source: Internet
Author: User
Tags cmath

The Bellman_ford algorithm is used to find positive or negative loops!

Introduction to Algorithms:

24.1 The Bellman-ford algorithm

the  bellman-ford algorithm  solves the Single-source shortest-paths problem in The which edge weights may negative. Given a weighted, directed graph  G  = ( V ,   E ) with source  s  and weight function  w  :  E  →  R , the Bellman-ford Algorithm returns a Boolean value indicating whether or not there be a negative-weight cycle that's reachable from Urce. If There is such a cycle, the algorithm indicates that no solution exists. If There is no such cycle, the algorithm produces the shortest paths and their weights.

The algorithm uses relaxation, progressively decreasing an estimate  d [ v ] on the weight of a Shor Test path from the source , s  to each vertex  v  ∈  v  until it Achieves the actual shortest-path weight  δ ( s ,   v ). The algorithm returns TRUE if and only if the graph contains no negative-weight cycles that is reachable from the source.

 Bellman-ford ( g ,  W ,  s ) 1 initialize-single-source ( G ,  s ) 2  For   i  1 to  |  V  [ G ]| - do for  each edge ( u ,  v ) ∈  E  [ G ]4  do RELAX ( u ,  v ,  w ) 5  for  each edge ( u ,  v ) ∈  E  [ Em>g ]6  do if   d  [ v ] >  d  [ u ] +  w  ( u ,  v ) 7  then return  FALSE8  return  TRUE 

Figure 24.4 shows the execution of the Bellman-ford algorithm on a graph with 5 vertices. After initializing the Dandπvalues of all vertices on line 1, the algorithm makes | V| –1 passes over the edges of the graph. Each pass was one iteration of the for loops of lines 2-4 and consists of relaxing each edge of the graph once. Figures 24.4 (b)-(e) Show the state of the algorithm after each of the four passes over the edges. After making | V|-1 passes, lines 5-8 check for a negative-weight cycle and return the appropriate Boolean value. (We'll see a little later why this check works.)

(Click the picture to enlarge)

Figure 24.4:the Execution of the Bellman-ford algorithm. The source is vertex  s . the  d  values is shown within the vertices, and shaded edges indicate, predecessor Values:if Edge ( U, v ) is shaded, thenπ[ v ] =  u . The particular example, each pass relaxes the edges in the order ( T, x ), ( T, y ), ( T, Z ), ( Em>x, T ), ( y, x ), ( y, z ), ( z, x ), ( Z, S ), ( s, T ), ( s, y ). (a) The situation just before the first pass over the edges. (b)-(e) The situation after each successive pass through the edges. the  d  andπvalues in part (e) is the final values. The Bellman-ford algorithm returns TRUE in this example.

The Bellman-ford algorithm runs in time O(v E), since the initialization on line 1 takesθ (v) t IME, each of the | V| –1 passes over the edges in lines 2-4 takesθ (E) time, and the for loop of lines 5-7 takes O( C7>e) time.

Topic:

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, C AB, R BA and C Ba-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<=10 3.
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<=r ate<=10 2, 0<=commission<=10 2.
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 10 4.

Output

If Nick can increase his wealth, output YES, in other case output No. to the output file.
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<sstream>#include<algorithm>#include<queue>#include<deque>#include<iomanip>#include<vector>#include<cmath>#include<map>#include<stack>#include<Set>#include<fstream>#include<memory>#include<list>#include<string>using namespaceStd;typedefLong Longll;typedef unsignedLong LongULL;#defineMAXN 105#defineN 33#defineMOD 10000007#defineINF 1000000009Const DoubleEPS = 1e-9;Const DoublePI = ACOs (-1.0);/*that is, looking for a positive ring starting from a given state .*/structedge{intu, v; DoubleCost and rate; Edge (int_u,int_v,Double_cost,Double_rate): U (_u), V (_v), Cost (_cost), rate (_rate) {}};vector<edge>E;DoubleDIS[MAXN];intN, M, s;Doublenum;BOOLBellman_ford (intSDoublenum) {memset (DIS,0,sizeof(DIS)); Dis[s]=num;  for(inti =0; I < n; i++)    {        BOOLf =false;//no slack .         for(intj =0; J < E.size (); J + +)        {            intU = e[j].u, v =e[j].v; Doublec = e[j].cost, r =e[j].rate; if(Dis[v] < (Dis[u]-c) *R) {f=true; DIS[V]= (Dis[u]-c) *R; }        }        if(!f)return false; }     for(intj=0; J<e.size (); j + +)        if(DIS[E[J].V] < (DIS[E[J].U]-e[j].cost) *e[j].rate) {            return true; }    return false;}intMain () { while(SCANF ("%D%D%D%LF", &n, &m, &s, &num)! =EOF) {        intA, B; Doublerab, cab, RBA, CBA;  for(inti =0; I < m; i++) {scanf ("%D%D%LF%LF%LF%LF", &a, &b, &rab, &cab, &rba, &CBA);            E.push_back (Edge (A, B, cab, Rab));        E.push_back (Edge (b, a, CBA, RBA)); }        if(Bellman_ford (S, num)) printf ("yes\n"); Elseprintf ("no\n"); }}

Bellman_ford algorithm Currency Exchange POJ1860

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.