-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_flow.py
59 lines (44 loc) · 1.37 KB
/
max_flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import sys
from collections import deque
INF = int(1e9)
def solve(n, graph, c, src, dst):
result = 0
f = [[0]*(n+1) for _ in range(n+1)]
# O(VE^2)
while True: # O(VE)
v = [-1] * (n+1)
q = deque()
q.append(src)
while q: # O(E)
now = q.popleft()
for next in graph[now]:
if c[now][next] > f[now][next] and v[next] == -1:
q.append(next)
v[next] = now # path trace
if next == dst: break
if v[dst] == -1: break # 모든 길 탐색 완료
flow = INF
tmp = dst
while tmp != src:
# 흐를 수 있는 유량의 최소값
flow = min(flow, c[v[tmp]][tmp] - f[v[tmp]][tmp])
tmp = v[tmp]
tmp = dst
while tmp != src:
f[v[tmp]][tmp] += flow
f[tmp][v[tmp]] -= flow
tmp = v[tmp]
result += flow
print(result)
return
if __name__ == "__main__":
n, e = map(int, input().split())
graph = [[]*(n+1) for _ in range(n+1)]
c = [[0]*(n+1) for _ in range(n+1)]
for _ in range(e):
a, b, cost = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
c[a][b] = cost
src, dst = map(int, input().split())
solve(n, graph, c, src, dst)