Reverse application of Bellman-Ford algorithm-positive loop check
/** \ Brief poj 1860 Bellman-Ford ** \ Param date 2014/7/24 * \ Param state AC * \ return memory 708 K time 141 Ms **/# include <iostream> # include <fstream> # include <cstring> using namespace STD; struct rateandcom {// public: int A; int B; double rate; double com ;}; // map [maxn]; const int maxn = 101; rateandcom map [101*2]; double dis [maxn]; int N; // Number of currency types int m; // number of exchange points int S; // hold the second currency Double V; // The second currency principal int alledge; bool Bellman_ford () {memset (DIS, 0, sizeof (DIS); DIS [s] = V;/* Relax */bool flag; For (INT I = 1; I <= N-1; I ++) {flag = false; For (Int J = 0; j <alledge; j ++) if (DIS [map [J]. b] <(DIS [map [J]. a]-map [J]. com) * map [J]. rate) {dis [map [J]. b] = (DIS [map [J]. a]-map [J]. com) * map [J]. rate; flag = true;} If (! Flag) break;} For (int K = 0; k <alledge; k ++) if (DIS [map [K]. b] <(DIS [map [K]. a]-map [K]. com) * map [K]. rate) return true; return false;} int main () {// cout <"Hello world! "<Endl; // freopen (" input.txt "," r ", stdin); // while (scanf (" % d % F ", & N, & M, & S, & V )! = EOF) while (CIN> N> m> S> V) {alledge = 0; For (INT I = 0; I <m; I ++) {int A, B; double Rab; double cab; double RBA; double CBA; // CIN> A> B> map [a] [B]. rate> map [a] [B]. commission //> map [B] [a]. rate> map [B] [a]. commission; CIN> A> B> Rab> cab> RBA> CBA; Map [alledge]. A = A; Map [alledge]. B = B; Map [alledge]. rate = Rab; Map [alledge]. COM = cab; alledge ++; Map [alledge]. A = B; Map [alledge]. B = A; Map [alledge]. rate = RBA; Map [alledge]. COM = CBA; alledge ++;} // Bellman-Ford if (bellman_ford () cout <"yes" <Endl; else cout <"no" <Endl;} return 0 ;}
Reprinted please indicate the source: http://blog.csdn.net/greenapple_shan/article/details/38307879