code: def kuhn_munkres(G):      # maximum profit bipartite matching in O(n^4)
      
      assert len(G) == len(G[0])
      n = len(G)
      mu = [None] * n                 # Empty matching
      mv = [None] * n
      lu = [max(row) for row in G]    # Trivial labels
      lv = [0] * n
      for u0 in range(n):
          if mu[u0] is None:          # Free node
              while True:
                  au = [False] * n    # Empty alternating tree
                  av = [False] * n
                  if improve_matching(G, u0, mu, mv, au, av, lu, lv):
                      break
                  improve_labels(G, au, av, lu, lv)
      return (mu,  sum(lu) + sum(lv))
docs: Maximum profit perfect matching
  
      for minimum cost perfect matching just inverse the weights
  
      :param G: squared weight matrix of a complete bipartite graph
      :complexity: :math:`O(n^4)`