Add max flow#10
Conversation
|
|
||
| def bfs() -> bool: | ||
| fill(level, self._n) | ||
| queue = [] |
There was a problem hiding this comment.
Tried reusing this queue, stack and edge_stack, though the difference wasn't significant.
$ hyperfine 'python ./atcoder/maxflow.py < input' 'python ./atcoder/maxflow2.py < input' -w 10 -r 30
Benchmark #1: python ./atcoder/maxflow.py < input
Time (mean ± σ): 164.2 ms ± 4.2 ms [User: 157.1 ms, System: 6.8 ms]
Range (min … max): 156.6 ms … 171.8 ms 30 runs
Benchmark #2: python ./atcoder/maxflow2.py < input
Time (mean ± σ): 167.4 ms ± 4.0 ms [User: 159.8 ms, System: 7.3 ms]
Range (min … max): 160.5 ms … 177.8 ms 30 runs
Summary
'python ./atcoder/maxflow.py < input' ran
1.02 ± 0.04 times faster than 'python ./atcoder/maxflow2.py < input'
|
|
| from typing import NamedTuple, Optional, List | ||
|
|
||
|
|
||
| class MaxFlow: |
There was a problem hiding this comment.
@not522 What do you prefer fro the name? Maybe rename to MFGraph to match the original impl?
| def min_cut(self, s: int) -> List[bool]: | ||
| visited = [False] * self._n | ||
| stack = [s] | ||
| visited[s] = True |
There was a problem hiding this comment.
Note: The original impl had visited[v] = True after stack.pop() instead of this.
| queue.append(e.dst) | ||
| return False | ||
|
|
||
| def dfs(lim) -> int: |
There was a problem hiding this comment.
Note: Unlike the original recursive implementation, this version uses stack and augment on a single path.
| src: int | ||
| dst: int | ||
| cap: int | ||
| flow: int |
There was a problem hiding this comment.
Used src, dst instead of from, to because from is a reserved word in python.
| assert 0 <= t < self._n | ||
| assert s != t | ||
| if flow_limit is None: | ||
| flow_limit = sum(e.cap for e in self._g[s]) |
There was a problem hiding this comment.
Note: Used the sum of capacity of the edges leaving s instead of numeric_limits<T>::max().
|
Can you switch the target branch to |
Use key function Co-authored-by: Naoto Mizuno <naotomizuno@preferred.jp>
f17d1ee to
d70ad9b
Compare
More type hints Co-authored-by: Naoto Mizuno <naotomizuno@preferred.jp>
|
LGTM, thanks! |
https://atcoder.jp/contests/practice2/submissions/16651973
I'm not really a Pythonist. Please feel free to be nitpicky, or even add commits on the branch if that's easier.