code: def dinic(graph, capacity, source, target):
      
      assert source != target
      add_reverse_arcs(graph, capacity)
      Q = deque()
      total = 0
      n = len(graph)
      flow = [[0] * n for u in range(n)]   # flow initially empty
      while True:                   # repeat while we can increase
          Q.appendleft(source)
          lev = [None] * n          # build levels, None = inaccessible
          lev[source] = 0           # by BFS
          while Q:
              u = Q.pop()
              for v in graph[u]:
                  if lev[v] is None and capacity[u][v] > flow[u][v]:
                      lev[v] = lev[u] + 1
                      Q.appendleft(v)
  
          if lev[target] is None:   # stop if sink is not reachable
              return flow, total
          up_bound = sum(capacity[source][v] for v in graph[source]) - total
          total += _dinic_step(graph, capacity, lev, flow, source, target,
                               up_bound)
docs: Maximum flow by Dinic
  
      :param graph: directed graph in listlist or listdict format
      :param capacity: in matrix format or same listdict graph
      :param int source: vertex
      :param int target: vertex
      :returns: skew symmetric flow matrix, flow value
      :complexity: :math:`O(|V|^2 |E|)`