

"""simple docstring"""


from __future__ import annotations



def   _A					(	_a	:						list[float]     ,   _a	:						List[Any]				):



      """simple docstring"""






      print(f'Vertex\tShortest Distance from vertex {src}'				)
      for i, d in enumerate(_a				):
            print(f'{i}\t\t{d}'				)



def   _A					(	_a	:						list[dict[str, int]]     ,   _a	:						list[float]     ,   _a	:						int				):



      """simple docstring"""






      for j in range(_a				):
            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   _A					(	_a	:						list[dict[str, int]]     ,   _a	:						int     ,   _a	:						int     ,   _a	:						int				):



      """simple docstring"""






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

      for _ in range(vertex_count - 1				):
            for j in range(_a				):
                  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(_a     ,   _a     ,   _a				)
      if negative_cycle_exists:
            raise Exception("""Negative cycle found"""				)

      return distance


if __name__ == "__main__":
  import doctest

  doctest.testmod()

  UpperCAmelCase													=int(input("Enter number of vertices: ").strip())
  UpperCAmelCase													=int(input("Enter number of edges: ").strip())

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

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

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







"""simple docstring"""


class lowerCamelCase__   :



    '''simple docstring'''




    def __init__( self    )    ->      Union[str, Any]:
          A				  =		{}




    def UpperCamelCase__		( self    )    ->      None:
          print(self.vertex    )
          for i in self.vertex:
                print(lowerCamelCase_ ,""" -> """ ,""" -> """.join([str(lowerCamelCase_    ) for j in self.vertex[i]]    )    )




    def UpperCamelCase__		( self ,lowerCamelCase_ ,lowerCamelCase_    )    ->      None:
          # check if vertex is already present,
          if from_vertex in self.vertex:
                self.vertex[from_vertex].append(lowerCamelCase_    )
          else:
                # else make a new vertex
                A				  =		[to_vertex]




    def UpperCamelCase__		( self    )    ->      None:
          # visited array for storing already visited nodes
          A				  =		[False] * len(self.vertex    )

          # call the recursive helper function
          for i in range(len(self.vertex    )    ):
                if not visited[i]:
                      self.dfs_recursive(lowerCamelCase_ ,lowerCamelCase_    )




    def UpperCamelCase__		( self ,lowerCamelCase_ ,lowerCamelCase_    )    ->      None:
          # mark start vertex as visited
          A				  =		True

          print(lowerCamelCase_ ,end=""" """    )

          # Recur for all the vertices that are adjacent to this node
          for i in self.vertex:
                if not visited[i]:
                      self.dfs_recursive(lowerCamelCase_ ,lowerCamelCase_    )


if __name__ == "__main__":
  UpperCAmelCase													=Graph()
  g.add_edge(0, 1)
  g.add_edge(0, 2)
  g.add_edge(1, 2)
  g.add_edge(2, 0)
  g.add_edge(2, 3)
  g.add_edge(3, 3)

  g.print_graph()
  print("DFS:")
  g.dfs()

  # OUTPUT:
  # 0  ->  1 -> 2
  # 1  ->  2
  # 2  ->  0 -> 3
  # 3  ->  3
  # DFS:
  #  0 1 2 3




