Dijkstra Algorithm

Given sample test case is showing correct output but after submission, it is showing run error. Please do help.

For unreachable nodes, print -1.
Consider a testcase like
1
4 2
1 2 24
1 4 20
1

The expected output is
24 -1 20

debug for this

I have the code as for you input it was showing null pointer exception, but now I have resolved it. Still it is showing wrong answer on submission. https://ide.codingblocks.com/s/431958

your code logic isn’t right .even for test case like

1
20 54
1 7 45
2 14 15
3 7 29
4 1 48
5 1 66
6 7 17
7 14 15
8 14 43
9 1 27
10 1 33
11 14 64
12 14 27
13 7 66
14 7 54
15 14 56
16 7 21
17 1 20
18 1 34
19 7 52
20 14 14
9 14 9
15 1 39
12 1 24
9 1 16
1 2 33
18 1 46
9 1 28
15 14 3
12 1 27
1 2 5
15 1 34
1 2 28
9 7 16
3 7 23
9 7 21
9 14 19
3 1 20
3 1 5
12 14 19
3 14 2
12 1 46
3 14 5
9 14 44
6 14 26
9 14 16
9 14 34
6 7 42
3 14 27
1 7 9
1 7 41
15 14 19
12 7 13
3 7 10
1 7 2
17

ans should be 20 25 25 68 86 39 22 70 36 53 91 35 88 27 30 43 54 74 41

In this problem, for a given graph G with N vertices, M undirected edges with integer weights between them, and special vertex S, the goal is to find the length of the shortest paths from S to each of all N vertices.

This problem is formally called Single Source Shortest Path problem.

If all edges of G have non-negative weights, the most well-known and also widely used method of solving this problem is to use Dijkstra’s algorithm implemented with a priority queue. In that case, the total time complexity of the algorithm is O(MlogN). This is true because each edge of the graph is examined exactly once, and such an examination can cause a constant number of updates in a priority queue containing either O(N) or O(M) vertices depending on the implementation. However, since each such operation on a priority queue has complexity proportional to the logarithm of its size, it does not matter if the queue contains O(N) or O(M) entries, since logM <= 2logN because M is at most N2 in this problem.
see this:

if this solves your doubt please mark it as resolved :slight_smile:

Can you please provide me the test case which the ide is using to check during submission? Because I have now made the necessary changes in the code and it is showing the correct output for the test you have provided me above. Still, on submission, it is showing me the wrong output error.

You can check the screenshot my output is exactly the same one you have provided me above.

https://ide.codingblocks.com/s/431958

sure
input:
https://cb-s3-bucket-ind-1.s3.amazonaws.com/testCases/900bba63-8782-4a6a-b6a5-aff690bbbc09
output:
https://cb-s3-bucket-ind-1.s3.ap-south-1.amazonaws.com/testCases/f85ab421-a489-4904-8105-617ca39308c4