網路流 網路流基礎 最大流

2021-10-08 13:04:26 字數 3553 閱讀 7224

學了坤m就對網路流有點感覺了

使網路的流量最大

應用很多,如可以求二分圖的最大匹配

左部左邊來個源點,右部右邊來個匯點

邊權設為1

11最大匹配數就等於網路的最大流量

bfs調整每條邊的流量(很像坤m)

增廣路:源點到匯點中剩餘容量為正的一條路徑

找到這條路徑,調整流量

那麼ek演算法的輪廓就出來了:

找到增廣路

調整流量

注意反向邊的影響:增大一條路徑的流量時,減小反向邊的流量

實際上我們關注的是剩餘容量

分析複雜度θ(n

m2)\theta(nm^2)

θ(nm2)

,實際可以處理1e3~1e4規模的網路

板可能會重邊

累計邊權

ek會t

#include

using

namespace std;

#define in read()

int in

typedef

long

long ll;

const ll inf=

2147483647

;const

int n=

205;

const

int m=

5e3+5;

int n,m,s,t;

int tot,first[n]

,nxt[m<<1]

,aim[m<<1]

;ll sub[n]

,wei[m<<1]

,maxflow;

int pre[n]

,rep[n]

[n];

bool vis[n]

;// sub: residue capability of edges over one dot (for edge it should be `wei`)

// pre: previous visited edge; for when updating we operate on `wei`

// rep: record repeated edge

void

ljb(

int u,

int v,

int w)

++tot;

nxt[tot]

=first[u]

; first[u]

=tot;

aim[tot]

=v; wei[tot]

=w; rep[u]

[v]=tot;

return;}

bool

bfs()}

// not found the sink, there's no augmented path

return

false;}

void

update()

maxflow+

=sub[t]

;return;}

signed

main()

while

(bfs()

)update()

;printf

("%lld"

,maxflow)

;return0;

}

一種眩暈的演算法

殘量網路:網路中所有節點以及剩餘容量大於零的邊構成的子圖

顯然ek每次遍歷乙個殘量網路,但只找出一條增廣路

正如停課回來的我考試炸裂一樣不優秀 想啥呢?優化它!

分層圖:bfs時節點層次d

dd表示源點到節點最少經過的邊數,分層圖指滿足dv=

du+1

d_v=d_u+1

dv​=du

​+1的邊構成的子圖(說那麼複雜)

分層圖之於bfs,正如搜尋樹之於dfs,當然兩者還是有差別的

dinic的演算法流程:

bfs構建層次圖

dfs尋找增廣路,回溯時更新剩餘流量

dfs過程還可剪枝

分析複雜度θ(n

2m)\theta(n^2m)

θ(n2m)

,實際可處理1e4~1e5規模的網路,效率很高

特別地,dinic處理二分圖最大匹配複雜度θ(m

n)\theta(m\sqrt n)

θ(mn​)

還是那個板

這∞

\infin

∞開long long上界會掛,開1<<30會掛,然鵝開21474836471<<29它就不掛

#include

using

namespace std;

#define in read()

int in

typedef

long

long ll;

const ll inf=

2147483647

;const

int n=

205;

const

int m=

5e3+5;

int n,m,s,t;

ll maxflow,wei[m<<1]

;int tot,first[n]

,nxt[m<<1]

,aim[m<<1]

,dis[n]

,rep[n]

[n],d[n]

;void

ljb(

int u,

int v,

int w)

++tot;

nxt[tot]

=first[u]

; first[u]

=tot;

aim[tot]

=v; wei[tot]

=w; rep[u]

[v]=tot;

return;}

bool

bfs()}

// cannot reach the sink

return

false;}

ll dinic

(int u,ll flow)

}return flow-rest;

}int

main()

ll flow;

while

(bfs()

)while

(flow=

dinic

(s,inf)

) maxflow+

=flow;

printf

("%lld"

,maxflow)

;return0;

}

板都背不到?

網路流 最大流

網路流 題記 網路流是最近講過的最迷演算法 網路流 network flows 是一種模擬水流的解決問題方法,與線性規劃密切相關。非常重視選手在網路流上的建模技巧,畫圖是非常關鍵的。1 最大流 問題引入 有n條溝渠,與水坑s t相連,匯聚成m個點,第i條溝渠的水流的流量為c i 每乙個點的流入量和流...

網路流(最大流)

我們圍繞一道題來講解吧 藍橋杯 演算法訓練 網路流裸題 首先,何為網路流最大流問題?可行流 對於每條路線 u,v 上給定乙個實數f u,v 滿足 0 f u,v c u,v 則稱f u,v 為路線 u,v 的流量。而對於一組具有源點和匯點,且總流出量 總流入量,則稱為網路中的一條可行流。拿例子來說 ...

網路流 最大流

dinic 最大流 head x 從0開始記 這樣便於找反向邊 異或即可 當前弧優化 include include include include include include include define ll long long define ull unsigned long long d...