Test instructions for half a day:
Give a direction graph, with edge right, SRC,DST. The maximum stream from src to DST is calculated, and the ratio of the traffic from SRC to the maximum DST flow is calculated.
1#include <cstdio>2#include <cstring>3#include <queue>4#include <vector>5 #defineOO 0x3f3f3f3f6 #defineMAXN 10107 using namespacestd;8 9 structEdge {Ten intu, V, F, cap; OneEdge (intUintVintFintcap): U (U), V (v), F (f), Cap (CAP) {} A }; - structDinic { - intN, SRC, DST; theVector<edge>Edge; -vector<int>G[MAXN]; - intDEP[MAXN], CUR[MAXN], sig; - + voidInitintNintSrcintDST) { - This->n =N; + This->SRC =src; A This->DST =DST; at for(intu=1; u<=n; u++ ) - g[u].clear (); - edge.clear (); - } - voidAdd_edge (intUintVintf) { - G[u].push_back (Edge.size ()); in Edge.push_back (Edge (u,v,f,f)); - G[v].push_back (Edge.size ()); toEdge.push_back (Edge (V,u,0, F)); + } - BOOLBFs () { thequeue<int>Qu; *memset (DEP,0,sizeof(DEP)); $ Qu.push (SRC);Panax NotoginsengDEP[SRC] =1; - while( !Qu.empty ()) { the intu=Qu.front (); + Qu.pop (); A for(intt=0; T<g[u].size (); t++ ) { theEdge &e =Edge[g[u][t]]; + if(E.F &&!)DEP[E.V]) { -DEP[E.V] = dep[e.u]+1; $ Qu.push (E.V); $ } - } - } the returnDEP[DST]; - }Wuyi intDfsintUintAintminc) { the if(U==DST | | a==0 ) { -sig =Max (Minc, SIG); Wu returnA; - } About intRemain=a, past=0, na; $ for(int&t=cur[u]; T<g[u].size (); t++ ) { -Edge &e =Edge[g[u][t]]; -Edge &ve = edge[g[u][t]^1]; - if(dep[e.v]==dep[e.u]+1&& (na=Dfs (E.v,min (remain,e.f), Min (minc,e.cap)))) { ARemain-=na; +Past + =na; theE.F-=na; -Ve.f + =na; $ if(remain==0) Break; the } the } the returnpast; the } - voidMaxflow (int&tot,int&SIG) { inTot = sig =0; the This->sig =0; the while(BFS ()) { Aboutmemset (cur,0,sizeof(cur)); theTot + =DFS (src,oo,oo); the } thesig = This-Sig; + } - }; the Bayi intN, M; the dinic D; the intMain () { - intT; -scanf"%d", &T); the while(t-- ) { the intcas, SRC, DST; thescanf"%d%d%d%d%d", &cas, &n, &m, &SRC, &DST); theSrc++, dst++; - d.init (n, SRC, DST); the for(intI=1, U,v,w; i<=m; i++ ) { thescanf"%d%d%d", &u, &v, &W); theu++, v++;94 D.add_edge (U, V, W); the } the inttot, SIG; the D.maxflow (Tot, SIG);98printf"%d%.3lf\n"CasDouble(TOT)/Double(SIG)); About } -}
View Code
HDU 4240 Maximum flow path