


"""simple docstring"""

# Author: OMKAR PATHAK, Nwachukwu Chidiebere

# Use a Python dictionary to construct the graph.
from __future__ import annotations

from pprint import pformat
from typing import Generic, TypeVar

UpperCAmelCase__					=TypeVar("T")





class  lowerCamelCase__    (		Generic[T]     ):
      def __init__(		self						:						List[Any]   ,							A_						:						bool = True				):






       '''simple docstring'''


       __lowercase						=						{}  # dictionary of lists
       __lowercase						=						directed
      def SCREAMING_SNAKE_CASE_						(		self						:						Any   ,							A_						:						T   ,							A_						:						T				):






       '''simple docstring'''


       if not self.directed:  # For undirected graphs
        # if both source vertex and destination vertex are both present in the
        # adjacency list, add destination vertex to source vertex list of adjacent
        # vertices and add source vertex to destination vertex list of adjacent
        # vertices.
        if source_vertex in self.adj_list and destination_vertex in self.adj_list:
         self.adj_list[source_vertex].append(A_				)
         self.adj_list[destination_vertex].append(A_				)
        # if only source vertex is present in adjacency list, add destination vertex
        # to source vertex list of adjacent vertices, then create a new vertex with
        # destination vertex as key and assign a list containing the source vertex
        # as it's first adjacent vertex.
        elif source_vertex in self.adj_list:
         self.adj_list[source_vertex].append(A_				)
         __lowercase						=						[source_vertex]
        # if only destination vertex is present in adjacency list, add source vertex
        # to destination vertex list of adjacent vertices, then create a new vertex
        # with source vertex as key and assign a list containing the source vertex
        # as it's first adjacent vertex.
        elif destination_vertex in self.adj_list:
         self.adj_list[destination_vertex].append(A_				)
         __lowercase						=						[destination_vertex]
        # if both source vertex and destination vertex are not present in adjacency
        # list, create a new vertex with source vertex as key and assign a list
        # containing the destination vertex as it's first adjacent vertex also
        # create a new vertex with destination vertex as key and assign a list
        # containing the source vertex as it's first adjacent vertex.
        else:
         __lowercase						=						[destination_vertex]
         __lowercase						=						[source_vertex]
       else:  # For directed graphs
        # if both source vertex and destination vertex are present in adjacency
        # list, add destination vertex to source vertex list of adjacent vertices.
        if source_vertex in self.adj_list and destination_vertex in self.adj_list:
         self.adj_list[source_vertex].append(A_				)
        # if only source vertex is present in adjacency list, add destination
        # vertex to source vertex list of adjacent vertices and create a new vertex
        # with destination vertex as key, which has no adjacent vertex
        elif source_vertex in self.adj_list:
         self.adj_list[source_vertex].append(A_				)
         __lowercase						=						[]
        # if only destination vertex is present in adjacency list, create a new
        # vertex with source vertex as key and assign a list containing destination
        # vertex as first adjacent vertex
        elif destination_vertex in self.adj_list:
         __lowercase						=						[destination_vertex]
        # if both source vertex and destination vertex are not present in adjacency
        # list, create a new vertex with source vertex as key and a list containing
        # destination vertex as it's first adjacent vertex. Then create a new vertex
        # with destination vertex as key, which has no adjacent vertex
        else:
         __lowercase						=						[destination_vertex]
         __lowercase						=						[]

       return self






      def __repr__(		self						:						Optional[int]				):






       '''simple docstring'''


       return pformat(self.adj_list				)




"""simple docstring"""

from __future__ import annotations


def lowerCAmelCase_   (					UpperCamelCase__						:			list[float]					, UpperCamelCase__						:			Any			):






 """simple docstring"""







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


def lowerCAmelCase_   (					UpperCamelCase__						:			list[dict[str, int]]					, UpperCamelCase__						:			list[float]					, UpperCamelCase__						:			int			):






 """simple docstring"""







 for j in range(UpperCamelCase__			):
  __lowercase				,		__lowercase				,		__lowercase						=						(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 lowerCAmelCase_   (					UpperCamelCase__						:			list[dict[str, int]]					, UpperCamelCase__						:			int					, UpperCamelCase__						:			int					, UpperCamelCase__						:			int			):






 """simple docstring"""







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

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

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

 __lowercase						=						check_negative_cycle(UpperCamelCase__					, UpperCamelCase__					, UpperCamelCase__			)
 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)
