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