Dijkstra's Algorithm Problem Statement

Given an undirected graph with 'V' vertices (labeled 0, 1, ..., V-1) and 'E' edges, where each edge connects two nodes ('X', 'Y') and has a weight that denotes the distance between nodes 'X' and 'Y'.

Your task is to calculate the shortest path distance from the source node (labeled as 0) to all vertices in the graph.

Input:

The first line contains an integer 'T' indicating the number of test cases to run. Each test case starts with a line containing two integers 'V' and 'E', representing the number of vertices and edges, respectively.
Following this, each of the next 'E' lines contains three space-separated integers 'X', 'Y', and 'DISTANCE', representing a node 'X', a node 'Y', and the distance between them.

Output:

For each test case, output a single line with 'V' space-separated integers that denote the shortest distance for each node from node 0 to node 'V'-1. If a node is disconnected, output 2147483647 for that node. Separate outputs with a new line for each test case.

Example:

Input:
4 5
0 1 5
0 2 8
1 2 9
1 3 2
2 3 6

Output:
0 5 8 7

Constraints:

  • 1 <= T <= 50
  • 1 <= V <= 1000
  • 1 <= E <= 3000
  • 0 <= X, Y < V
  • 1 <= DISTANCE[X][Y] <= 10^5
Note:
Do not print the output in the function; it is handled automatically. Implement the function as required.
Be the first one to answer
Add answer anonymously...
Directi Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter