



from __future__ import annotations

from collections import deque

class 					_A :



       def __init__(	self							:  List[Any]  ,	__SCREAMING_SNAKE_CASE							:  list[str]):


           '''simple docstring'''

           __a		 =       []
           self.adlist.append(
               {'''value''': '''''', '''next_states''': [], '''fail_state''': 0, '''output''': []})

           for keyword in keywords:
               self.add_keyword(__SCREAMING_SNAKE_CASE)
           self.set_fail_transitions()



       def 	_lowerCamelCase    (	self							:  Tuple  ,	__SCREAMING_SNAKE_CASE							:  int  ,	__SCREAMING_SNAKE_CASE							:  str):


           '''simple docstring'''

           for state in self.adlist[current_state]["next_states"]:
               if char == self.adlist[state]["value"]:
                   return state
           return None



       def 	_lowerCamelCase    (	self							:  List[str]  ,	__SCREAMING_SNAKE_CASE							:  str):


           '''simple docstring'''

           __a		 =       0
           for character in keyword:
               __a		 =       self.find_next_state(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE)
               if next_state is None:
                   self.adlist.append(
                       {
                           '''value''': character,
                           '''next_states''': [],
                           '''fail_state''': 0,
                           '''output''': [],
                       })
                   self.adlist[current_state]["next_states"].append(len(self.adlist) - 1)
                   __a		 =       len(self.adlist) - 1
               else:
                   __a		 =       next_state
           self.adlist[current_state]["output"].append(__SCREAMING_SNAKE_CASE)



       def 	_lowerCamelCase    (	self							:  Dict):


           '''simple docstring'''

           __a		 =       deque()
           for node in self.adlist[0]["next_states"]:
               q.append(__SCREAMING_SNAKE_CASE)
               __a		 =       0
           while q:
               __a		 =       q.popleft()
               for child in self.adlist[r]["next_states"]:
                   q.append(__SCREAMING_SNAKE_CASE)
                   __a		 =       self.adlist[r]['''fail_state''']
                   while (
                       self.find_next_state(__SCREAMING_SNAKE_CASE  ,	self.adlist[child]['''value''']) is None
                       and state != 0
                   ):
                       __a		 =       self.adlist[state]['''fail_state''']
                   __a		 =       self.find_next_state(
                       __SCREAMING_SNAKE_CASE  ,	self.adlist[child]['''value'''])
                   if self.adlist[child]["fail_state"] is None:
                       __a		 =       0
                   __a		 =       (
                       self.adlist[child]['''output''']
                       + self.adlist[self.adlist[child]['''fail_state''']]['''output''']
                   )





       def 	_lowerCamelCase    (	self							:  Optional[Any]  ,	__SCREAMING_SNAKE_CASE							:  str):


           '''simple docstring'''

           __a		 =       {}  # returns a dict with keywords and list of its occurrences
           __a		 =       0
           for i in range(len(__SCREAMING_SNAKE_CASE)):
               while (
                   self.find_next_state(__SCREAMING_SNAKE_CASE  ,	string[i]) is None
                   and current_state != 0
               ):
                   __a		 =       self.adlist[current_state]['''fail_state''']
               __a		 =       self.find_next_state(__SCREAMING_SNAKE_CASE  ,	string[i])
               if next_state is None:
                   __a		 =       0
               else:
                   __a		 =       next_state
                   for key in self.adlist[current_state]["output"]:
                       if key not in result:
                           __a		 =       []
                       result[key].append(i - len(__SCREAMING_SNAKE_CASE) + 1)
           return result


if __name__ == "__main__":
 import doctest

 doctest.testmod()










from __future__ import annotations





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





def   __snake_case			(    _UpperCAmelCase     ,				_UpperCAmelCase     ,				_UpperCAmelCase       ):
    for j in range(_UpperCAmelCase       ):
        __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   __snake_case			(    _UpperCAmelCase     ,				_UpperCAmelCase     ,				_UpperCAmelCase     ,				_UpperCAmelCase       ):
    __a		 =       [float('''inf'''       )] * vertex_count
    __a		 =       0.0

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

    return distance


if __name__ == "__main__":
 import doctest

 doctest.testmod()

 __snake_case		:Dict   	=  int(input('''Enter number of vertices: ''').strip())
 __snake_case		:Any   	=  int(input('''Enter number of edges: ''').strip())

 __snake_case		:list[dict[str, int]]   	=  [{} for _ in range(E)]

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

 __snake_case		:List[str]   	=  int(input('''\nEnter shortest path source:''').strip())
 __snake_case		:Optional[Any]   	=  bellman_ford(graph, V, E, source)
 print_distance(shortest_distance, 0)





