Test instructions: A weighted graph of n-point m edges is given, and the least-averaged circuit is obtained.
I want to use DFS to find the ring (really too young), in the comparison to find the average weight of each ring, but the code can not be realized, I think it is not right
Later reading = = Good ingenious method, using the dichotomy method, first record the maximum weight of the M-bar UB
Then you can guess a mid, just to determine if there is a loop with an average less than mid, assuming there is a loop with a K-side, the weights for each edge of the loop are w1,w,2,w3,----, wk
So
w1+w2+w3+----+wk<k*mid
And because Lenovo to Bellman_ford can solve the negative ring, the conversion of the above-
(W1-mid) + (W2-mid) + (W3-mid) +----(Wk-mid) <0
This first converts each side W (A, B) into a W (A, B)-mid, and then judges whether there is a negative ring in the "new" graph
When you look at the time there are two do not understand, is the most beginning to judge why to use Ub+1,
is because ub+1 is the worst answer, it can make every side of the most negative, if in this case can not find negative ring, then there must be no negative ring
Then, if the negative ring can be found in the ub+1 condition, then the binary finds the smallest ring of the mean, until it reaches the precision of the loop exit.
Code learning for the standard range = =
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One #definemod=1e9+7; A using namespacestd; - -typedefLong LongLL; the Const intINF =0x7fffffff; - Const intmaxn=10005; - - structedge{ + int from, to; - DoubleDist; + }; A at structbellmanford{ - intn,m; -Vector<edge>edges; -vector<int>G[MAXN]; - BOOLINQ[MAXN]; - DoubleD[MAXN]; in intP[MAXN]; - intCNT[MAXN]; to + voidInitintN) { - This->n=N; the for(intI=0; i<n;i++) g[i].clear (); * edges.clear (); $ }Panax Notoginseng - voidAddedges (int from,intTo,DoubleDist) { theEdges.push_back (Edge) { from, to,dist}); +m=edges.size (); Ag[ from].push_back (M-1); the } + - BOOLnegativecycle () { $queue<int>Q; $memset (INQ,0,sizeof(INQ)); -memset (CNT,0,sizeof(CNT)); - for(intI=0; i<n;i++) {d[i]=0; inq[0]=true; Q.push (i);} the - while(!Q.empty ()) {Wuyi intu=Q.front (); Q.pop (); theinq[u]=false; - for(intI=0; I<g[u].size (); i++){ Wuedge& e=Edges[g[u][i]]; - if(d[e.to]>d[u]+e.dist) { Aboutd[e.to]=d[u]+e.dist; $p[e.to]=G[u][i]; - if(!Inq[e.to]) { - Q.push (e.to); -inq[e.to]=true; A if(++cnt[e.to]>N) + return true; the } - } $ } the } the return false; the } the }; - in Bellmanford solver; the the BOOLTestDoublex) { About for(intI=0; i<solver.m;i++) thesolver.edges[i].dist-=x; the the BOOLret=solver.negativecycle (); + for(intI=0; i<solver.m;i++) -solver.edges[i].dist+=x; the returnret;Bayi } the the intMain () { - intT; -scanf"%d",&T); the for(intKase=1; kase<=t;kase++){ the intn,m; thescanf"%d%d",&n,&m); the solver.init (n); - intub=0; the while(m--){ the intu,v,w; thescanf" %d%d%d", &u,&v,&w); u--;v--;ub=Max (ub,w);94 Solver. Addedges (U,V,W); the } theprintf"Case #%d:", Kase); the if(!test (ub+1)) printf ("No Cycle found.\n");98 Else{ About DoubleL=0, r=UB; - while(r-l>1e-3){101 Doublem=l+ (r-l)/2;102 if(Test (M)) R=m;ElseL=M;103 }104printf"%.2lf\n", L); the }106 }107 return 0;108}
View Code
UVa 11090 going in cycle!! "Bellman_ford"