Topic Portal
1 /*2 Shortest Way (Bellman_ford): The idea of seeking negative ring, but in turn, to find positive ring3 Detailed Explanation:http://blog.csdn.net/lyy289065406/article/details/66457784 */5#include <cstdio>6#include <iostream>7#include <algorithm>8#include <cstring>9#include <vector>Ten#include <cmath> One#include <queue> A#include <map> -#include <Set> - using namespacestd; the - Const intMAXN = -+Ten; - Const intINF =0x3f3f3f3f; - Const DoubleEPS = 1e-8; + structNODE - { + intu, v; A DoubleR, C; at}node[maxn*2]; - DoubleD[MAXN]; - - BOOLBellman_ford (intSintNintTotDoubleV) - { -memset (D,0,sizeof(d)); inD[s] =V; - BOOLFlag; to for(intI=1; i<=n-1; ++i) + { -Flag =false; the for(intj=1; j<=tot; ++j) * { $NODE e =Node[j];Panax Notoginseng if(D[E.V] < (D[E.U]-e.c) *E.R) - { theD[E.V] = (d[e.u]-e.c) * E.R; Flag =true; + } A } the if(!flag) Break; + } - $ for(intI=1; i<=tot; ++i) $ { -NODE e =Node[i]; - if(D[E.V] < (D[E.U]-e.c) * E.R)return true; the } - Wuyi return false; the } - Wu intMainvoid)//POJ 1860 Currency Exchange - { About //freopen ("a.in", "R", stdin); $ - intN, M, s; - DoubleV; - while(~SCANF ("%D%D%D%LF", &n, &m, &s, &V)) A { + inttot =0; the for(intI=1; i<=m; ++i) - { $ intx, y; the Doublerab, cab, RBA, CBA; thescanf ("%D%D%LF%LF%LF%LF", &x, &y, &rab, &cab, &rba, &CBA); the //printf ("%d%d%lf%lf%lf%lf\n", X, Y, Rab, cab, RBA, CBA); theNODE[++TOT].U = x; NODE[TOT].V =y; -NODE[TOT].R = rab; NODE[TOT].C =cab; innode[++tot].u = y; NODE[TOT].V =x; theNODE[TOT].R = RBA; NODE[TOT].C =CBA; the } About the if(Bellman_ford (S, N, tot, V)) puts ("YES"); the ElsePuts ("NO"); the } + - return 0; the}
Shortest circuit (bellman_ford) POJ 1860 Currency Exchange