How this Solution works even if in priority queue the values stored are not weights.I think for Dijikstra to work we should maintain a priority queue which give the node whose distance is shortest from the source but in this implementation the priority queue gives the smallest node number
LINK:http://mirror.codeforces.com/contest/20/submission/30960352
PLZZ HELP ME UNDERSTAND THIS SOLUTION AND IF I CHANGE THE PRIORITY QUEUE TO QUEUE IT GIVE TLE AT TEST NUMBER 28 I DIDN'T ABLE TO UNDERSTAND HOW PRIORITY QUEUE IS BENIFICIAL HERE INSTEAD OF A NORMAL QUEUE
To begin with ,Dijkstra algorithm is a Greedy Algorithm.
Therefore while using Dijkstra algorithm we have to use a priority queue (or similar data structure ) so that we can process the edge which have greater weight's before the edges that have a smaller weight.
Reason for doing so :
"if(dis[u] + w < dis[v])"
this particular statement check's if the previous path has more weight than the current path. If we process the heavier edges first then this process becomes very much easier.That is the reason for using priority queue in Dijkstra.
But the code store the node number instead of wieghts in the priority queue
In the question you are trying to solve , The author's solution is just storing the neighboring nodes.
If u check TC # 28 , if you do not use a priority queue , u will have to process 50000 nodes. but using priority queue , 50000 will be above all the other nodes.
And also there is a direct path from node 1 to node 50000.
In the originally linked solution, how does the priority queue know which nodes are closest (and thus should be processed first) when the priority queue only stores the node (instead of say, a pair<node, weight>)?
yes thats my question REDUX.There should be weight stored too along with the node number
Weak tests
I think the problem might have weak test case, and the TL is too tight for priority queue to pass (because of doubled runtime).
Counter case:
The linked code (and my own "AC" solution) prints "1 5" instead of "1 2 3 4 5".