Title Link: http://poj.org/problem?id=1860
Test instructions is to give you n kinds of currency, the following m type of exchange, with the first s currency of the V-dollar. It's impossible to have a revaluation if you finally go through any conversion. The following gives you the currency U and the currency v,r1 is the exchange rate of U to V, C1 is a handling fee of U to V, R2 is the exchange rate of V to u, C2 is the handling fee of V to U. Converted Money B = (conversion before the money a-c) * R.
I do it with SPFA, and I keep on relaxing. If there is a positive loop, or if the initial amount of money in the middle process rises, it means that it will appreciate. If there is a negative ring, the condition that does not meet the relaxation, slowly will pop up the queue, also will not appreciate.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5 using namespacestd;6 Const intMAXN =1005;7 structData {8 intnext, to;9 DoubleR, C;Ten}EDGE[MAXN *3]; One intHEAD[MAXN], cont; A DoubleD[MAXN]; - - voidInitintN) { the for(inti =0; I <= N; i++) { -Head[i] =-1; -D[i] =0; - } +Cont =0; - } + AInlinevoidAddintU,intVDoubleRDoublec) { atEdge[cont].next =Head[u]; -Edge[cont].to =v; -EDGE[CONT].R =R; -EDGE[CONT].C =C; -Head[u] = cont++; - } in - BOOLSPFA (intS,DoubleV) { toQueue <int>que; + while(!Que.empty ()) { - Que.pop (); the } * Que.push (s); $D[s] =V;Panax Notoginseng while(!Que.empty ()) { - inttemp =Que.front (); the Que.pop (); + for(inti = head[temp]; ~i; i =Edge[i].next) { A Doublex = EDGE[I].R * (D[temp]-edge[i].c); the if(x > D[edge[i].to]) {//Slack +D[edge[i].to] =x; - Que.push (edge[i].to); $ if(D[s] > V)//increment returns true directly $ return true; - } - } the } - return false;Wuyi } the - intMain () Wu { - intN, m, S, u, v; About DoubleV, R, C; $ while(~SCANF ("%d %d%d%lf", &n, &m, &s, &V)) { - init (n); - for(inti =0; I < m; i++) { -scanf"%d%d%lf%lf", &u, &v, &r, &c); A Add (U, V, R, c); +scanf"%LF%LF", &r, &c); the Add (V, U, R, c); - } $ if(SPFA (S, V)) theprintf"yes\n"); the Else theprintf"no\n"); the } -}
POJ 1860 Currency Exchange (SPFA Slack)