Given an undirected, connected and weighted graph G(V, E) with V number of vertices (which are numbered from 0 to V-1) and E number of edges. Find and print the shortest distance from the source vertex (i.e. Vertex 0) to all other vertices (including source vertex also) using Dijkstra's Algorithm.

 Code : Dijkstra's Algorithm

Given an undirected, connected and weighted graph G(V, E) with V number of vertices (which are numbered from 0 to V-1) and E number of edges.

Find and print the shortest distance from the source vertex (i.e. Vertex 0) to all other vertices (including source vertex also) using Dijkstra's Algorithm.

Input Format :

Line 1: Two Integers V and E (separated by space)

Next E lines : Three integers ei, ej and wi, denoting that there exists an edge between vertex ei and vertex ej with weight wi (separated by space)

Output Format :

For each vertex, print its vertex number and its distance from source, in a separate line. The vertex number and its distance needs to be separated by a single space.

Note : Order of vertices in output doesn't matter.

Constraints :

2 <= V, E <= 10^5

Time Limit: 1 sec

Sample Input 1 :

4 4

0 1 3

0 3 5

1 2 1

2 3 8

Sample Output 1 :

0 0

1 3

2 4

3 5

SOURCE CODE : 









Post a Comment

0 Comments