code: def solve_tsp(dists: dict) -> float:
      """Solve the TSP problem
  
      Args:
          dists (dict): the distance matrix between each nodes. Each item in the
              dict is a pair (node A, node B) to the distance from A to B.
  
      Returns:
          float: the optimal cost
      """
      # Get the unique nodes from the distance matrix
      nodes = set()
      for pair in dists.keys():
          nodes.add(pair[0])
          nodes.add(pair[1])
  
      # Generate all possible routes (permutations of nodes)
      routes = permutations(nodes)
  
      # Initialize the optimal cost as infinite
      optimal_cost = float("inf")
      optimal_route = None
  
      # Iterate through all possible routes
      for route in routes:
          cost = 0
          # Calculate the cost of the current route
          for i in range(len(route)):
              current_node = route[i]
              next_node = route[(i + 1) % len(route)]
              cost += dists[(current_node, next_node)]
  
          # Update the optimal cost if the current cost is smaller
          if cost < optimal_cost:
              optimal_cost = cost
              optimal_route = route
  
      print("Cost:", optimal_cost, "with route", optimal_route)
      return optimal_cost
  
imports: import math
  import pdb
  import random
  import sys
  from itertools import combinations, permutations
label_desc: Use image node_id 1 for calling a global function with example usage: solve_tsp(dists) and returns: optimal_cost