def _insertion_cost(distances, previous_node, next_node, inserted_node):
  """Calculates insertion costs of inserting a node into a tour.

  Args:
    distances: A distance matrix.
    previous_node: The node before the inserted node. Can be a vector.
    next_node: The node after the inserted node. Can be a vector.
    inserted_node: The node to insert.

  Returns:
    The extra tour cost(s) when inserting the node at the given location(s).
  """
  return (distances[previous_node, inserted_node]
          + distances[inserted_node, next_node]
          - distances[previous_node, next_node])