POJ 1860 Currency Exchange

Source: Internet
Author: User

POJ 1860 Currency exchange[★★☆☆☆] graph theory Shortest way Bellman
    • Main topic:

      There are a variety of currency, exchange between the currency can be exchanged, which requires a handling fee, when you use 100A coins
      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 the amount of S currency to be exchanged eventually
      Whether the amount of S currency received can be increased.

    • Sample Example

      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
      Output:
      YES

    • Problem Solving Ideas:

      The variant of the Bellman algorithm turns the search for negative loops into a positive ring.
      I used a forced loop to find Mon[s] > V, the problem is more friendly, or overtime.
      It is recommended to use loops to determine if there is no positive ring. This way, in the case of extreme data, many cycles are missing.

    • Code
#include <iostream>#include <algorithm>using namespace STD;structEdge {intFrom, to;DoubleR, C;//rate, cost};intN, M, S;DoubleV;edge e[205];intCteDoublemon[ the];//moneyBOOLBellman () { while(1) {BOOLUpdate =false; for(inti =0; I < CTE; i++) {Edge e = e[i];if(Mon[e.from] >0&& Mon[e.to] < (mon[e.from]-e.c) *E.R) {mon[e.to] = (mon[e.from]-e.c) *E.R; Update =true; }        }if(!update) Break;if(Mon[s] > V) Break; }if(Mon[s] > V)return true;return false;}intMain () {CTE =0;Cin>> N >> M >> S >> V; for(inti =1; I <= N; i++) {Mon[i] =0; } Mon[s] = V; for(inti =0; i < M; i++) {Edge te;Cin>> te.from >> te.to >> te. R >> te.        C e[cte++] = te;Cin>> te. R >> te. Cintt = Te.from; Te.from = te.to;        te.to = t;    e[cte++] = te; }if(Bellman ())cout<<"yes\n";Else cout<<"no\n";return 0;}

POJ 1860 Currency Exchange

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.