


If there is no path between 'src' and 'ith' vertex, the value at 'ith' index in the answer array will be 10^8.

3 3 1
1 2 2
1 3 2
2 3 -1
In the above graph:
The length of the shortest path between vertex 1 and vertex 1 is 1->1 and the cost is 0.
The length of the shortest path between vertex 1 and vertex 2 is 1->2 and the cost is 2.
The length of the shortest path between vertex 1 and vertex 3 is 1->2->3 and the cost is 1.
Hence we return [0, 2, 1].
It's guaranteed that the graph doesn't contain self-loops and multiple edges. Also, the graph does not contain negative weight cycles.
The first line contains three single space-separated integers βNβ, βMβ, and βsrcβ denoting the number of vertices, the number of edges in the directed graph, the source vertex, respectively.
The following βMβ lines contain three single space-separated integers βuβ, βvβ, and βwβ, denoting an edge from vertex βuβ to vertex βvβ, having weight βwβ.
Return an integer denoting the shortest path length from βsrcβ to βdestβ. If no path is possible, return 10^9.
You do not need to print anything; it has already been taken care of. Just implement the given function.
In this algorithm, we have a source vertex and we find the shortest path from source to all the vertices.
For this, we will create an array of distances D[1...N], where D[i] stores the distance of vertex βiβ from the source vertex. Initially all the array values contain an infinite value, except βD[source]β, βD[source]β = 0.
The algorithm consists of several iterations and in each iteration, we try to produce relaxation in the edges. Relaxation means reducing the value of βD[i]β.
For this, we will iterate on the edges of the graph letβs consider an edge (βuβ, βvβ, βwβ) :
The algorithm claims that βN-1β iterations are enough to find the distances of the vertices from the source vertex.
We also know that the graph doesnβt contain negative weight cycles. Hence we do not need to check it explicitly.
Algorithm: