1、网络流模板最大流:Ford&Fulkerson算法#include #include #include using namespace std;const int maxint = 100000000;#define MAX 410int n,m;int capMAXMAX,prevMAX,s,t,flowMAXMAX;int find() int QMAX,head=0,tail=0; memset(prev,-1,sizeof(prev); Qtail+ = s; prevs = s; while (head tail) int p = Qhead+; for (int i=1; i0)
2、previ = p; if (i = t) return 1; Qtail+ = i; return 0;int maxflow() int ex,i,ret = 0; while (find() ex = maxint; for (i=t; i!=s; i=previ) if (ex capprevii - flowprevii) ex = capprevii - flowprevii; for (i=t; i!=s; i=previ) flowprevii += ex; flowiprevi -= ex; ret += ex; return ret;最大流:Dinic算法#include
3、#include using namespace std;#define MAXN 20010#define inf 0x7fffffffstruct node int next, c, f, other;N;vectormapMAXN; / 原图vectorlever_mapMAXN; / 层次图int queMAXN*1, leverMAXN, preMAXN, hashMAXN, dMAXN;int s, t;void init() int i; for(i = s;i = t; i+) mapi.clear(); void add(int u, int v, int c) N.next
4、 = v; N.c = c; N.other = mapv.size();N.f = 0; mapu.push_back(N); N.next = u; N.c = 0; N.other = mapu.size()-1;N.f = 0; mapv.push_back(N);bool bulid() int head = 0, tail = 0, cur, next, i, j; for(i = s; i = t; i+) lever_mapi.clear(); memset(lever, -1, sizeof(lever); quetail+ = s; levers = 0; while(he
5、ad tail) cur = quehead+; for(i = 0; i N.f) if(leverN.next = -1) quetail+ = N.next; leverN.next = levercur + 1; if(leverN.next = levercur + 1) lever_mapcur.push_back(i); return levert!=-1;int Dinic() int i, j , ans = 0, len, tmp; while(bulid() memset(hash, 0, sizeof(hash); while(!hashs) ds = inf; pre
6、s = -1; for(i = s ; i!= t & i!= -1; i = j) len = lever_mapi.size(); while(len & hashmapilever_mapilen-1.next) lever_mapi.pop_back();len-; if(!len) hashi = 1; j = prei; continue; j = mapilever_mapilen-1.next; prej = i; dj = min(di, mapilever_mapilen - 1.c - mapilever_mapilen-1.f); if(i = t) ans += dt
7、; tmp = dt; while(i != s) j = prei; len = lever_mapjlever_mapj.size()-1; mapjlen.f += tmp; if(mapjlen.f = mapjlen.c) lever_mapj.pop_back(); mapimapjlen.other.f -= tmp; i = j; return ans;int main() int n, m, i, a, b, w;/ freopen(Dinic.in, r, stdin); scanf(%d %d, &n, &m); s = 0; t = n + 1; init(); for
8、(i = 1; i =n; i+) scanf(%d %d, &a, &b); add(s, i, a); add(i, t, b); for(i = 1; i= m; i+) scanf(%d %d %d, &a, &b, &w); add(a, b, w); add(b, a, w); printf(%dn, Dinic();最大流:ek算法#include #include #include using namespace std;/const int N = 128;/const int INF = 1 28;template class Graph private: int v, s
9、, t, prevN; T capNN, flowNN; bool bfs();public: void clear(); void setMaxNode(int n) v = n; void insert(int nu, int nv, int c) capnunv += c; T getFlow(int nu, int nv) return flownunv; T maxFlow(int, int);template void Graph:clear() v = 0; for(int i = 0; i N; i+) for(int j = 0; j N; j+) capij = T(0);
10、template bool Graph:bfs() queue Q; bool vstN = false ; Q.push(s); vsts = true; while(!Q.empty() int u = Q.front(); Q.pop(); for(int i = 0; i v; i+) if(!vsti & capui != flowui) Q.push(i); previ = u; vsti = true; if(i = t) return true; return false;template T Graph:maxFlow(int ss, int tt) s = ss; t =
11、tt; for(int i = 0; i v; i+) for(int j = 0; j v; j+) flowij = T(0); T f = 0; while(bfs() T ex = T(INF); for(int c = t; c != s; c = prevc) ex ?= capprevcc-flowprevcc; for(int c = t; c != s; c = prevc) flowprevcc += ex; flowcprevc -= ex; f += ex; return f;最大流:dsp #include #include #include #include usi
12、ng namespace std;/const int N = 5120;/const int INF = 1 28;class Edge public: int u, v, cuv, cvu, flow; Edge() Edge(int cu, int cv, int ccu, int ccv) : u(cu), v(cv), cuv(ccu), cvu(ccv), flow(0) int other(int p) const return p = u ? v : u; int cap(int p) const return p = u ? cuv-flow : cvu+flow; void
13、 addFlow(int p, int f) flow += (p = u ? f : -f); ;class Graph private: vector eg; vector netN; Edge* prevN; int v, s, t; int hN, hn2*N, curN; void initNet(); void initFlow(); void initHeight(); void gapHeuristic(int);public: void clear() eg.clear(); v = 0; void setMaxNode(int cv) v = cv; void insert
14、(int cu, int cv, int ccu, int ccv = 0) eg.push_back(Edge(cu, cv, ccu, ccv); int maxFlow(int, int);void Graph:initHeight() memset(h, 0, sizeof(h); memset(hn, 0, sizeof(hn); for(int i = 0; i v; i+) hi = v; queue Q; Q.push(t); ht = 0; while(!Q.empty() int p = Q.front(); Q.pop(); for(int i = netp.size()
15、-1; i = 0; i-) int u = netpi-other(p), ec = netpi-cap(u); if(ec != 0 & hu = v) hu = hp+1; Q.push(u); for(int i = 0; i v; i+) hnhi+;void Graph:gapHeuristic(int k) if(hnk != 0) return; for(int i = 0; i k) hi = v; for(int i = k; i v; i+) hnv += hni; hni = 0; void Graph:initNet() for(int i = 0; i = 0; i
16、-) netegi.u.push_back(&egi); netegi.v.push_back(&egi); void Graph:initFlow() initNet(); initHeight(); for(int i = 0; i v; i+) curi = neti.size()-1;int Graph:maxFlow(int ss, int tt) s = ss; t = tt; initFlow(); int c = s, preN, flow = 0; pres = -1; while(hs = 0; curc-) if(netccurc-cap(c) != 0 & hc = h
17、netccurc-other(c)+1) break; if(curc = 0; i-) if(netci-cap(c) != 0) mh other(c); if(mh = INF) hc = v; else hc = mh+1; curc = netc.size()-1; hnoh-; hnhc+; gapHeuristic(oh); if(c != s) c = prec; else int p = netccurc-other(c); prevp = netccurc; prep = c; c = p; if(c = t) int ex = INF; for(; c != s; c =
18、 prec) ex cap(prec); for(c = t; c != s; c = prec) prevc-addFlow(prec, ex); flow += ex; c = s; return flow;最大流:hlpp算法#include #include #include #include using namespace std;/const int N = 512;/const int INF = 1 ?= h; int remove(); bool empty() const return level = 0 & indexlevel = -1) level-; return
19、r;class Graph private: vector eg; vector netN; int n, v, s, t; NodeList list; int hN, hn2*N, eN, curN; void initNet(); void initFlow(); void initHeight(); void push(int); void relabel(int); void discharge(int); void gapHeuristic(int);public: void clear() eg.clear(); v = 0; void setMaxNode(int cv) v
20、= cv; void insert(int cu, int cv, int ccu, int ccv = 0) eg.push_back(Edge(cu, cv, ccu, ccv); int maxFlow(int, int);void Graph:gapHeuristic(int k) if(hnk != 0 | k = v+1) return; for(int i = 0; i k & hi = v & i != s) hnhi-; hnv+1+; hi = v+1; void Graph:initNet() for(int i = 0; i = 0; i-) netegi.u.push
21、_back(&egi); netegi.v.push_back(&egi); void Graph:initHeight() memset(h, 0, sizeof(h); memset(hn, 0, sizeof(hn); memset(e, 0, sizeof(e); for(int i = 0; i v; i+) hi = v; queue Q; Q.push(t); ht = 0; while(!Q.empty() int p = Q.front(); Q.pop(); for(int i = netp.size()-1; i = 0; i-) int u = netpi-other(
22、p), ec = netpi-cap(u); if(ec != 0 & hu = v & u != s) hu = hp+1; Q.push(u); for(int i = 0; i v; i+) hnhi+;void Graph:initFlow() initNet(); initHeight(); for(int i = 0; i = 0; curs-) push(s);void Graph:push(int u) Edge* te = netucuru; int ex = te-cap(u), p = te-other(u); if(ex = 0) return; if(u != s) ex addFlow(u, ex); ep += ex;void Graph:relabel(int u) int mh = 2*v, oh = hu; for(int i = netu.size()-1; i = 0; i-) i
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1