You have been given an undirected, connected graph of ‘V’ vertices (labelled from 0 to V-1) and ‘E’ edges. Each edge connecting two nodes 'u' and 'v' has a weight denoting the distance between them.
Your task is to find the shortest path distance from the source node 'S' to all the vertices. You have to return a list of integers denoting the shortest distance between each vertex and source vertex 'S'.
Note:
1. There are no self-loops(an edge connecting the vertex to itself) in the given graph.
2. There are no parallel edges i.e no two vertices are directly connected by more than 1 edge.
For Example:
Input:
4 5
0 1 5
0 2 8
1 2 9
1 3 2
2 3 6

The source node is node 0.
The shortest distance from node 0 to node 0 is 0.
The shortest distance from node 0 to node 1 is 5. In the above figure, the green path represents this distance. The path goes from node 0->1, giving distance = 5.
The shortest distance from node 0 to node 2 is 8. In the above figure, the pink path represents this distance. The path goes from node 0->2, giving distance = 8.
The shortest distance from node 0 to node 3 is 7. In the above figure, the yellow path represents this distance. The path goes from node 0->1->3, giving distance = 7.
Input format:
The first line contains three space-separated integers ‘V’, ‘E’ and 'S', denoting the number of vertices, the number of edges in the undirected graph and starting vertex, respectively.
The next ‘E’ lines each contain three single space-separated integers ‘u’, ‘v’, and ‘distance’, denoting a node ‘u’, a node ‘v’, and the distance between nodes (u, v), respectively.
Output format:
The only line contains the list of integers denoting the shortest distance between each node and source vertex 'S'.
5 7 0
0 1 7
0 2 1
0 3 2
1 2 3
1 3 5
1 4 1
3 4 7
0 4 1 2 5

The source node is node 0.
The shortest distance from node 0 to node 0 is 0.
The shortest distance from node 0 to node 1 is 4. In the above figure, the green path represents this distance. The path goes from node 0->2->1, giving distance = 1+3 = 4.
The shortest distance from node 0 to node 2 is 1. In the above figure, the red path represents this distance. The path goes from node 0->2, giving distance = 1
The shortest distance from node 0 to node 3 is 2. In the above figure, the pink path represents this distance. The path goes from node 0->3, giving distance = 2.
The shortest distance from node 0 to node 4 is 5. In the above figure, the yellow path represents this distance. The path goes from node 0->2->1->4, giving distance = 1+3+1 = 5.
3 3 2
1 0 9
2 1 8
0 2 4
4 8 0
1 <= 'V' <= 10^5
1 <= 'E' <= 10^5
1 <= distance between vertices <= 10^5
Time limit: 1 second
Consider the minimum distance vertices and relax their adjacent vertices.
The idea is to maintain two arrays, one stores the visited nodes, and the other array stores the distance (element at index ‘i’ denotes the distance from node 0 to node ‘i’). We pick an unvisited minimum distance vertex, then update the distance of all its adjacent vertices considering the path from source to an adjacent vertex to pass through the picked minimum distance vertex. Repeat the process till all nodes are visited.
Steps:
O(V^2), where ‘V’ is the number of vertices in a graph.
In the worst case, using a minimum distance node, distances of ‘V’ vertices are updated. So, using ‘V’ minimum distance nodes, vertices are updated ‘V^2’ times.
O(V^2), where ‘V’ is the number of vertices in a graph.
The adjacency matrix takes the size of the order ‘V^2’.