Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Maximum Flow

A flow network is a directed graph which has a $source$ and a $sink$. In a flow network, each edge $(u, v)$ has a capacity $c(u, v)$. Each edge receives a flow, but the amount of flow on the edge can not exceed the corresponding capacity. Find the maximum flow from the $source$ to the $sink$.

Input

A flow network is given in the following format.

$|V|\;|E|$

$u_0\;v_0\;c_0$

$u_1\;v_1\;c_1$

:

$u_{|E|-1}\;v_{|E|-1}\;c_{|E|-1}$

$|V|$, $|E|$ is the number of vertices and edges of the flow network respectively. The vertices in $G$ are named with the numbers 0, 1,..., $|V|-1$. The source is 0 and the sink is $|V|-1$.

$u_i$, $v_i$, $c_i$ represent $i$-th edge of the flow network.
A pair of $u_i$ and $v_i$ denotes that there is an edge from $u_i$ to $v_i$ and $c_i$ is the capacity of $i$-th edge.

Output

Print the maximum flow.

Constraints

 $2 \leq |V| \leq 100$
	

 $1 \leq |E| \leq 1000$
	

 $0 \leq c_i \leq 10000$
	

Sample Input 1

4 5
0 1 2
0 2 1
1 2 1
1 3 1
2 3 2

Sample Output 1

3



Sample input:
4 5
0 1 2
0 2 1
1 2 1
1 3 1
2 3 2


Sample output:
3

Submission status: Runtime Error

Solution code:
# Acceptance of input

import sys

file_input = sys.stdin

V, E = map(int, file_input.readline().split())

adj_mat = [[0] * V for i in range(V)]

for line in file_input:
    u, v, c = map(int, line.split())
    adj_mat[u][v] = c

# Dinic's algorithm

import collections

# BFS for residual capacity network to construct level graph
def bfs(start, goal, parent):
    level = [V] * V
    queue = collections.deque()
    queue.append(start)
    level[start] = 0
    while queue:
        u = queue.popleft()
        next_level = level[u] + 1
        for v, flow in enumerate(adj_mat[u]):
            if (level[v] >= next_level) and (flow > 0):
                queue.append(v)
                level[v] = next_level
                parent[v].append(u)
    return level[goal]

# DFS for level graph to construct blocking flow
# search in reverse direction from sink
def dfs(goal, path, parent, aug_path_flow, blocking_flow):
    v = path[-1]
    if v == goal:
        blocking_flow[0] += aug_path_flow
        for x, y in zip(path[1:], path[:-1]):
            adj_mat[x][y] -= aug_path_flow
            adj_mat[y][x] += aug_path_flow
    else:
        for u in parent[v]:
            path.append(u)
            dfs(goal, path, parent, min(aug_path_flow, adj_mat[u][v]), blocking_flow)
            path.pop()

def dinic(source, sink):
    parent = [[] for i in range(V)]
    max_flow = 0
    while bfs(source, sink, parent) != V:
        aug_path_flow = 10000
        blocking_flow = [0]
        path = [sink]
        dfs(source, path, parent, 5, blocking_flow)
        max_flow += blocking_flow[0]
    return max_flow

# output
print(dinic(0, V - 1))

Submission status: Runtime Error

Solution code:
# Acceptance of input

import sys

file_input = sys.stdin

V, E = map(int, file_input.readline().split())

adj_mat = [[0] * V for i in range(V)]

for line in file_input:
    u, v, c = map(int, line.split())
    adj_mat[u][v] = c

# Dinic's algorithm

import collections

# BFS for residual capacity network to construct level graph
def bfs(start, goal, parent):
    level = [V] * V
    queue = collections.deque()
    queue.append(start)
    level[start] = 0
    while queue:
        u = queue.popleft()
        next_level = level[u] + 1
        for v, flow in enumerate(adj_mat[u]):
            if (level[v] >= next_level) and (flow > 0):
                queue.append(v)
                level[v] = next_level
                parent[v].append(u)
    return level[goal]

# DFS for level graph and construct blocking flow
def dfs(goal, path, parent, blocking_flow):
    v = path[-1]
    if v == goal:
        aug_path_flow = 10000
        for x, y in zip(path[1:], path[:-1]):
            aug_path_flow = min(aug_path_flow, adj_mat[x][y])
        for x, y in zip(path[1:], path[:-1]):
            adj_mat[x][y] -= aug_path_flow
            adj_mat[y][x] += aug_path_flow
        blocking_flow[0] += aug_path_flow
    else:
        for u in parent[v]:
            path.append(u)
            dfs(goal, path, parent, blocking_flow)
            path.pop()

def dinic(source, sink):
    parent = [[] for i in range(V)]
    max_flow = 0
    while bfs(source, sink, parent) != V:
        blocking_flow = [0]
        path = [sink]
        # search in reverse direction from sink
        dfs(source, path, parent, blocking_flow)
        max_flow += blocking_flow[0]
    return max_flow

# output
print(dinic(0, V - 1))

Submission status: Runtime Error

Solution code:
from collections import deque

INF = 10 ** 20

V, E = map(int, input().split())
net = [[0] * V for _ in range(V)]
for _ in range(E):
  u, v, c = map(int, input().split())
  net[u][v] = c

start = 0
goal  = V - 1
def check_net(node, path, capacity):
  for i in range(V):
    cap = net[node][i]
    if cap != 0:
      if i == goal:
        return (path + [i], min(cap, capacity))
      else:
        return check_net(i, path + [i], min(cap, capacity))

  else:
    return ([], 0)

def update_net(path, capacity):
  for fromi, toi in zip(range(len(path) - 1), range(1, len(path))):
    fr, to = path[fromi], path[toi]
    net[fr][to] -= capacity
    net[to][fr] += capacity

i = 0
while True:
  path, cap = check_net(start, [], INF)
  if path == []:
    break
  update_net(path, cap)

print(sum(net[start]))