P3376 模板 網路最大流dinic演算法

2022-04-06 20:37:40 字數 2470 閱讀 9572

如題,給出乙個網路圖,以及其源點和匯點,求出其網路最大流。

輸入格式:

第一行包含四個正整數n、m、s、t,分別表示點的個數、有向邊的個數、源點序號、匯點序號。

接下來m行每行包含三個正整數ui、vi、wi,表示第i條有向邊從ui出發,到達vi,邊權為wi(即該邊最大流量為wi)

輸出格式:

一行,包含乙個正整數,即為該網路的最大流。

輸入樣例#1:

4 5 4 3

4 2 30

4 3 20

2 3 20

2 1 30

1 3 40

輸出樣例#1:

50
時空限制:1000ms,128m

資料規模:

對於30%的資料:n<=10,m<=25

對於70%的資料:n<=200,m<=1000

對於100%的資料:n<=10000,m<=100000

樣例說明:

題目中存在3條路徑:

4-->2-->3,該路線可通過20的流量

4-->3,可通過20的流量

4-->2-->1-->3,可通過10的流量(邊4-->2之前已經耗費了20的流量)

故流量總計20+20+10=50。輸出50。

dinic演算法,需要注意許多點:

1 #include2 #include3

4using

namespace

std;56

const

int n = 20100;7

const

int inf =1e9;89

struct

edgee[500100

];12

13int q[5000100

],head[n],dis[n],cur[n];

14int s,t,n,m,l,r,tot = 1;15

16 inline char

nc()

20 inline int

read()

26 inline void add_edge(int u,int v,int

w) 30 inline bool

bfs()

34 l = 1;r = 0

;35 q[++r] =s;

36 dis[s] = 0;37

while (l <=r) 46}

47}48return

false;49

}50int dfs(int u,int

flow)

62} 63}

64if (used != flow) dis[u] = -1;65

return

used;66}

67 inline int

dinic()

73int

main()

79 printf("%d"

,dinic());

80return0;

81 }

1 #include2 #include3 #include4 #include5

using

namespace

std;67

const

int maxn = 100100;8

const

int inf =1e9;910

struct

edge

13 edge(int tt,int cc,int nn)

14 }e[1000100

];15 queueq;

1617

inthead[maxn],dis[maxn];

18int s,t,n,m,tot = 1;19

20int

read()

2124

while (ch>='

0'&&ch<='

9')

25return x*f;26}

27bool

bfs()

2844}45

}46if (dis[t]!=-1) return

true;47

return

false;48

}49int dfs(int u,int

low)

5064}65

if (!used) dis[u] = -1;66

return

used;67}

68int

dinic()

6975

intmain()

7686 printf("%d"

,dinic());

87return0;

88 }

很早以前的**

P3376 模板 網路最大流

網路流用於解決流量問題 網路流 所有弧上流量的集合f 稱為該容量網路的乙個網路流。1 定義 帶權的有向圖g v,e 滿足以下條件,則稱為網路流圖 flow network 僅有乙個入度為0的頂點s,稱s為源點。僅有乙個出度為0的頂點t,稱t為匯點。每條邊的權值都為非負數,稱為該邊的容量,記作c i,...

P3376 模板 網路最大流

ek演算法 個人感覺沒有dinic好理解 1 edmonds karp演算法2 時間複雜度o n m m 3 include4 include5 include6 include7 include8 using namespace std 910 const int n 10005 11 const...

P3376 模板 網路最大流

如題,給出乙個網路圖,以及其源點和匯點,求出其網路最大流。輸入格式 第一行包含四個正整數n m s t,分別表示點的個數 有向邊的個數 源點序號 匯點序號。接下來m行每行包含三個正整數ui vi wi,表示第i條有向邊從ui出發,到達vi,邊權為wi 即該邊最大流量為wi 輸出格式 一行,包含乙個正...