



'''simple docstring'''


from collections.abc import Callable







class 	UpperCAmelCase	:


 '''simple docstring'''




 def __init__(   self							,  lowerCAmelCase_ = None) ->      None:



      """simple docstring"""







      a_       		=[]
      # Stores indexes of each item for supporting updates and deletion.
      a_       		={}
      # Stores current size of heap.
      a_       		=0
      # Stores function used to evaluate the score of an item on which basis ordering
      # will be done.
      a_       		=key or (lambda lowerCAmelCase_: x)




 def       lowercase_  (   self							,  lowerCAmelCase_) ->      int | None:



      """simple docstring"""







      return int((i - 1) / 2) if i > 0 else None




 def       lowercase_  (   self							,  lowerCAmelCase_) ->      int | None:



      """simple docstring"""







      a_       		=int(2 * i + 1)
      return left if 0 < left < self.size else None




 def       lowercase_  (   self							,  lowerCAmelCase_) ->      int | None:



      """simple docstring"""







      a_       		=int(2 * i + 2)
      return right if 0 < right < self.size else None




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      None:



      """simple docstring"""







      a_		,						a_       		=(
          self.pos_map[self.arr[j][0]],
          self.pos_map[self.arr[i][0]],
      )
      # Then swap the items in the list.
      a_		,						a_       		=self.arr[j], self.arr[i]




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      bool:



      """simple docstring"""







      return self.arr[i][1] < self.arr[j][1]




 def       lowercase_  (   self							,  lowerCAmelCase_) ->      int:



      """simple docstring"""







      a_       		=self._left(lowerCAmelCase_)
      a_       		=self._right(lowerCAmelCase_)
      a_       		=i

      if left is not None and not self._cmp(lowerCAmelCase_							,  lowerCAmelCase_):
           a_       		=left
      if right is not None and not self._cmp(lowerCAmelCase_							,  lowerCAmelCase_):
           a_       		=right

      return valid_parent




 def       lowercase_  (   self							,  lowerCAmelCase_) ->      None:



      """simple docstring"""







      a_       		=self._parent(lowerCAmelCase_)
      while parent is not None and not self._cmp(lowerCAmelCase_							,  lowerCAmelCase_):
           self._swap(lowerCAmelCase_							,  lowerCAmelCase_)
           a_		,						a_       		=parent, self._parent(lowerCAmelCase_)




 def       lowercase_  (   self							,  lowerCAmelCase_) ->      None:



      """simple docstring"""







      a_       		=self._get_valid_parent(lowerCAmelCase_)
      while valid_parent != index:
           self._swap(lowerCAmelCase_							,  lowerCAmelCase_)
           a_		,						a_       		=valid_parent, self._get_valid_parent(lowerCAmelCase_)




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      None:



      """simple docstring"""







      if item not in self.pos_map:
           return
      a_       		=self.pos_map[item]
      a_       		=[item, self.key(lowerCAmelCase_)]
      # Make sure heap is right in both up and down direction.
      # Ideally only one of them will make any change.
      self._heapify_up(lowerCAmelCase_)
      self._heapify_down(lowerCAmelCase_)




 def       lowercase_  (   self							,  lowerCAmelCase_) ->      None:



      """simple docstring"""







      if item not in self.pos_map:
           return
      a_       		=self.pos_map[item]
      del self.pos_map[item]
      a_       		=self.arr[self.size - 1]
      a_       		=index
      self.size -= 1
      # Make sure heap is right in both up and down direction. Ideally only one
      # of them will make any change- so no performance loss in calling both.
      if self.size > index:
           self._heapify_up(lowerCAmelCase_)
           self._heapify_down(lowerCAmelCase_)




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      None:



      """simple docstring"""







      a_       		=len(self.arr)
      if arr_len == self.size:
           self.arr.append([item, self.key(lowerCAmelCase_)])
      else:
           a_       		=[item, self.key(lowerCAmelCase_)]
      a_       		=self.size
      self.size += 1
      self._heapify_up(self.size - 1)




 def       lowercase_  (   self) ->      tuple | None:



      """simple docstring"""







      return self.arr[0] if self.size else None




 def       lowercase_  (   self) ->      tuple | None:



      """simple docstring"""







      a_       		=self.get_top()
      if top_item_tuple:
           self.delete_item(top_item_tuple[0])
      return top_item_tuple






def  UpperCAmelCase_       (		):


     '''simple docstring'''









if __name__ == "__main__":
  import doctest

  doctest.testmod()







'''simple docstring'''


from __future__ import annotations






def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ):


     '''simple docstring'''







     print(F"""Vertex\tShortest Distance from vertex {src}"""       )
     for i, d in enumerate(lowercase__       ):
          print(F"""{i}\t\t{d}"""       )






def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ,	lowercase__       ):


     '''simple docstring'''







     for j in range(lowercase__       ):
          a_		,						a_		,						a_       		=(graph[j][k] for k in ["src", "dst", "weight"])
          if distance[u] != float("inf"       ) and distance[u] + w < distance[v]:
               return True
     return False






def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ,	lowercase__       ,	lowercase__       ):


     '''simple docstring'''







     a_       		=[float("inf"       )] * vertex_count
     a_       		=0.0

     for _ in range(vertex_count - 1       ):
          for j in range(lowercase__       ):
               a_		,						a_		,						a_       		=(graph[j][k] for k in ["src", "dst", "weight"])

               if distance[u] != float("inf"       ) and distance[u] + w < distance[v]:
                    a_       		=distance[u] + w

     a_       		=check_negative_cycle(lowercase__       ,	lowercase__       ,	lowercase__       )
     if negative_cycle_exists:
          raise Exception("Negative cycle found"       )

     return distance


if __name__ == "__main__":
  import doctest

  doctest.testmod()

  lowercase							       =			int(input('''Enter number of vertices: ''').strip())
  lowercase							       =			int(input('''Enter number of edges: ''').strip())

  lowercase							       =			[{} for _ in range(E)]

  for i in range(E):
    print('''Edge ''', i + 1)
    lowercase       ,      lowercase       ,      lowercase							       =			(
        int(x)
        for x in input('''Enter source, destination, weight: ''').strip().split(''' ''')
    )
    lowercase							       =			{'''src''': src, '''dst''': dest, '''weight''': weight}

  lowercase							       =			int(input('''\nEnter shortest path source:''').strip())
  lowercase							       =			bellman_ford(graph, V, E, source)
  print_distance(shortest_distance, 0)


