The exchange of the two currencies can be treated as two sides, and the Bellman_ford algorithm will be better after the graph is built.
The Bellman_ford algorithm can be used to handle negative edges, so it is possible to determine if a negative ring exists. In turn, it is possible to determine whether positive loops exist.
1 /*--------------------------------------------------------------------------------------*/2 //Helica ' s header3 //Second Edition4 //2015.11.75 //6#include <algorithm>7#include <iostream>8#include <cstring>9#include <ctype.h>Ten#include <cstdlib> One#include <cstdio> A#include <vector> -#include <string> -#include <queue> the#include <stack> -#include <cmath> -#include <Set> -#include <map> + - //Debug function for a n*m array + #defineDebug_map (n,m,g) printf ("\ n"); for (int i=0;i< (N); i++) A{ for(intj=0;j< (M); J + +){ atprintf"%d", G[i][j]);} printf"\ n");} - //Debug function for int,float,double,etc. - #defineDebug_var (X) cout<< #X "=" <<X<<endl; - /*--------------------------------------------------------------------------------------*/ - using namespacestd; - in Const intMAXN = -+Ten; - intn,m,t,s; to intTol; + DoubleV; - the structEdge * { $ intu,v;Panax Notoginseng DoubleC,r; -}e[Ten*MAXN]; the + DoubleDIST[MAXN]; A the BOOLBellman_ford (intStartintN) + { -memset (Dist,0,sizeofDist); $Dist[start] =V; $ - for(intI=1; i<n;i++) - { the BOOLFlag =false; - for(intj=0; j<tol;j++)if(DIST[E[J].V] < (DIST[E[J].U]-E[J].C) *E[J].R)Wuyi { theDIST[E[J].V] = (DIST[E[J].U]-E[J].C) *E[J].R; -Flag =true; Wu } - if(!flag)return false; About } $ - for(intj=0; j<tol;j++) - { - if(DIST[E[J].V] < (DIST[E[J].U]-E[J].C) *E[J].R) A return true; + } the return false; - } $ the intMain () the { the while(~SCANF ("%D%D%D%LF",&n,&m,&s,&V)) the { - intb; in Doubler1,r2,c1,c2; theTol =0; the for(intI=0; i<m;i++) About { thescanf"%D%D%LF%LF%LF%LF",&a,&b,&r1,&c1,&r2,&C2); theE[TOL].U = A; E[TOL].V =b; theE[TOL].C = C1; E[TOL++].R =R1; +E[TOL].U = b; E[TOL].V =A; -e[tol].c = C2; E[TOL++].R =R2; the }Bayi if(Bellman_ford (s,n)) theprintf"yes\n"); the Else -printf"no\n"); - the } the}
Poj1860-currency exchange-is sentenced to positive ring