

from __future__ import annotations

from queue import Queue





class       __UpperCAmelCase  :



   def __init__(							self							:  Tuple		):
     UpperCAmelCase :							dict[int, list[int]]								=				{}



   def  __magic_name__					(							self							:  Union[str, Any]		):
     for i in self.vertices:
       print(__A,	''' : ''',	''' -> '''.join([str(__A		) for j in self.vertices[i]]		)		)



   def  __magic_name__					(							self							:  Union[str, Any],	__A							:  int,	__A							:  int		):
     if from_vertex in self.vertices:
       self.vertices[from_vertex].append(__A		)
     else:
       UpperCAmelCase :							Union[str, Any]								=				[to_vertex]





   def  __magic_name__					(							self							:  Optional[Any],	__A							:  int		):
     UpperCAmelCase :							Union[str, Any]								=				set()

     # create a first in first out queue to store all the vertices for BFS
     UpperCAmelCase :							Queue								=				Queue()

     # mark the source node as visited and enqueue it
     visited.add(__A		)
     queue.put(__A		)

     while not queue.empty():
       UpperCAmelCase :							int								=				queue.get()

       # loop through all adjacent vertex and enqueue it if not yet visited
       for adjacent_vertex in self.vertices[vertex]:
         if adjacent_vertex not in visited:
           queue.put(__A		)
           visited.add(__A		)
     return visited


if __name__ == "__main__":
     from doctest import testmod

     testmod(verbose=True)

     _lowerCamelCase       :     Tuple           =							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()
     # 0  :  1 -> 2
     # 1  :  2
     # 2  :  0 -> 3
     # 3  :  3

     assert sorted(g.bfs(2)) == [0, 1, 2, 3]








def     a__  (	UpperCAmelCase							:    List[str]       ,				UpperCAmelCase							:    List[Any]    )						->		Optional[int]:
  print('''\nVertex Distance'''    )
  for i in range(UpperCAmelCase    ):
    if dist[i] != float('''inf'''    ):
      print(UpperCAmelCase       ,				'''\t'''       ,				int(dist[i]    )       ,				end='''\t'''    )
    else:
      print(UpperCAmelCase       ,				'''\t'''       ,				'''INF'''       ,				end='''\t'''    )
    print()

def     a__  (	UpperCAmelCase							:    Tuple       ,				UpperCAmelCase							:    Optional[int]       ,				UpperCAmelCase							:    Optional[int]    )						->		Any:
  UpperCAmelCase :							Union[str, Any]								=				float('''inf'''    )
  UpperCAmelCase :							str								=				-1
  for i in range(UpperCAmelCase    ):
    if (not vset[i]) and mdist[i] < min_val:
      UpperCAmelCase :							int								=				i
      UpperCAmelCase :							Union[str, Any]								=				mdist[i]
  return min_ind

def     a__  (	UpperCAmelCase							:    Union[str, Any]       ,				UpperCAmelCase							:    Optional[Any]       ,				UpperCAmelCase							:    str    )						->		Tuple:
  UpperCAmelCase :							str								=				[float('''inf'''    ) for _ in range(UpperCAmelCase    )]
  UpperCAmelCase :							Optional[int]								=				[False for _ in range(UpperCAmelCase    )]
  UpperCAmelCase :							List[Any]								=				0.0

  for _ in range(v - 1    ):
    UpperCAmelCase :							Any								=				min_dist(UpperCAmelCase       ,				UpperCAmelCase       ,				UpperCAmelCase    )
    UpperCAmelCase :							List[Any]								=				True

    for i in range(UpperCAmelCase    ):
      if (
          (not vset[i])
          and graph[u][i] != float('''inf'''    )
          and mdist[u] + graph[u][i] < mdist[i]
      ):
        UpperCAmelCase :							str								=				mdist[u] + graph[u][i]

  print_dist(UpperCAmelCase       ,				UpperCAmelCase    )


if __name__ == "__main__":
     _lowerCamelCase       :     Tuple           =							int(input("Enter number of vertices: ").strip())
     _lowerCamelCase       :     List[str]           =							int(input("Enter number of edges: ").strip())

     _lowerCamelCase       :     Optional[int]           =							[[float("inf") for i in range(V)] for j in range(V)]

     for i in range(V):
          _lowerCamelCase       :     Union[str, Any]           =							0.0

     for i in range(E):
          print("\nEdge ", i + 1)
          _lowerCamelCase       :     List[str]           =							int(input("Enter source:").strip())
          _lowerCamelCase       :     List[str]           =							int(input("Enter destination:").strip())
          _lowerCamelCase       :     List[Any]           =							float(input("Enter weight:").strip())
          _lowerCamelCase       :     Any           =							weight

     _lowerCamelCase       :     List[str]           =							int(input("\nEnter shortest path source:").strip())
     dijkstra(graph, V, gsrc)





