from collections.abc import Callable


class      lowerCamelCase_					:






    '''simple docstring'''






    def __init__(     self			,			__lowercase = None)	->     None:
        # Stores actual heap items.
        __UpperCamelCase					:list        =		[]
        # Stores indexes of each item for supporting updates and deletion.
        __UpperCamelCase					:dict        =		{}
        # Stores current size of heap.
        __UpperCamelCase					:Tuple        =		0
        # Stores function used to evaluate the score of an item on which basis ordering
        # will be done.
        __UpperCamelCase					:List[str]        =		key or (lambda __lowercase: x)







    def       UpperCamelCase__       (     self			,			__lowercase)	->     int | None:
        return int((i - 1) / 2) if i > 0 else None







    def       UpperCamelCase__       (     self			,			__lowercase)	->     int | None:
        __UpperCamelCase					:int        =		int(2 * i + 1)
        return left if 0 < left < self.size else None







    def       UpperCamelCase__       (     self			,			__lowercase)	->     int | None:
        __UpperCamelCase					:Any        =		int(2 * i + 2)
        return right if 0 < right < self.size else None







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase)	->     None:
        __UpperCamelCase					:Dict        =		(
            self.pos_map[self.arr[j][0]],
            self.pos_map[self.arr[i][0]],
        )
        # Then swap the items in the list.
        __UpperCamelCase					:Optional[int]        =		self.arr[j], self.arr[i]







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase)	->     bool:
        return self.arr[i][1] < self.arr[j][1]







    def       UpperCamelCase__       (     self			,			__lowercase)	->     int:
        __UpperCamelCase					:Dict        =		self._left(__lowercase)
        __UpperCamelCase					:Optional[int]        =		self._right(__lowercase)
        __UpperCamelCase					:Dict        =		i

        if left is not None and not self._cmp(__lowercase			,			__lowercase):
            __UpperCamelCase					:Optional[int]        =		left
        if right is not None and not self._cmp(__lowercase			,			__lowercase):
            __UpperCamelCase					:Tuple        =		right

        return valid_parent







    def       UpperCamelCase__       (     self			,			__lowercase)	->     None:
        __UpperCamelCase					:int        =		self._parent(__lowercase)
        while parent is not None and not self._cmp(__lowercase			,			__lowercase):
            self._swap(__lowercase			,			__lowercase)
            __UpperCamelCase					:Tuple        =		parent, self._parent(__lowercase)







    def       UpperCamelCase__       (     self			,			__lowercase)	->     None:
        __UpperCamelCase					:Optional[Any]        =		self._get_valid_parent(__lowercase)
        while valid_parent != index:
            self._swap(__lowercase			,			__lowercase)
            __UpperCamelCase					:Dict        =		valid_parent, self._get_valid_parent(__lowercase)







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase)	->     None:
        if item not in self.pos_map:
            return
        __UpperCamelCase					:Tuple        =		self.pos_map[item]
        __UpperCamelCase					:Tuple        =		[item, self.key(__lowercase)]
        # Make sure heap is right in both up and down direction.
        # Ideally only one of them will make any change.
        self._heapify_up(__lowercase)
        self._heapify_down(__lowercase)







    def       UpperCamelCase__       (     self			,			__lowercase)	->     None:
        if item not in self.pos_map:
            return
        __UpperCamelCase					:List[Any]        =		self.pos_map[item]
        del self.pos_map[item]
        __UpperCamelCase					:int        =		self.arr[self.size - 1]
        __UpperCamelCase					:Union[str, Any]        =		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(__lowercase)
            self._heapify_down(__lowercase)







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase)	->     None:
        __UpperCamelCase					:Union[str, Any]        =		len(self.arr)
        if arr_len == self.size:
            self.arr.append([item, self.key(__lowercase)])
        else:
            __UpperCamelCase					:Optional[Any]        =		[item, self.key(__lowercase)]
        __UpperCamelCase					:Optional[Any]        =		self.size
        self.size += 1
        self._heapify_up(self.size - 1)







    def       UpperCamelCase__       (     self)	->     tuple | None:
        return self.arr[0] if self.size else None







    def       UpperCamelCase__       (     self)	->     tuple | None:
        __UpperCamelCase					:Tuple        =		self.get_top()
        if top_item_tuple:
            self.delete_item(top_item_tuple[0])
        return top_item_tuple



def lowerCamelCase  (       ):
    '''simple docstring'''
















if __name__ == "__main__":
    import doctest

    doctest.testmod()






from __future__ import annotations



def lowerCamelCase  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     ):
    '''simple docstring'''







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



def lowerCamelCase  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     ):
    '''simple docstring'''







    for j in range(SCREAMING_SNAKE_CASE     ):
        __UpperCamelCase  ,	__UpperCamelCase  ,	__UpperCamelCase					:int        =		(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  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     ):
    '''simple docstring'''







    __UpperCamelCase					:List[Any]        =		[float('''inf'''     )] * vertex_count
    __UpperCamelCase					:str        =		0.0

    for _ in range(vertex_count - 1     ):
        for j in range(SCREAMING_SNAKE_CASE     ):
            __UpperCamelCase  ,	__UpperCamelCase  ,	__UpperCamelCase					:Dict        =		(graph[j][k] for k in ['''src''', '''dst''', '''weight'''])

            if distance[u] != float('''inf'''     ) and distance[u] + w < distance[v]:
                __UpperCamelCase					:Any        =		distance[u] + w

    __UpperCamelCase					:Tuple        =		check_negative_cycle(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     )
    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)





