ImageVerifierCode 换一换
格式:DOCX , 页数:20 ,大小:19.06KB ,
资源ID:6647904      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6647904.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(网络流模板.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

网络流模板.docx

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