What is wrong with following code?


it is passing sample test case,but is showing wrong answer on submission.

I suggest you to use vector instead of list because a list stores elements at non contiguous memory location i.e. it internally uses a doubly linked list, whereas, a vector stores elements at contiguous memory locations like an array.
Using a priority queue (min heap) for this question will be better as we just have to find the minimum distance from source.

that’s okay sir,but changing the data structure for storing graph will merely improve some time/space complexity a little bit.But why i am getting wrong answer(WA) that’s the problem ,if this would have been the case then i should have got tle or running out of memory or some error like that,with the current submission (whose link i provided in earlier message (using list)).

for which test case it is producing wrong answer i want to know that and also why it is producing that?

You need to pick nodes according to their distances from the source. So you will need to sort them accordingly and priority queue is most suitable for this purpose.


Check this and tell me if you are able to understand it.

that’s absolutely true,what i am saying is something else ,i know that choice of list is not the best choice in this case ,that’s okay but using list or priority queue should not convert WA into CA,it might change TLE to CA,but what i am getting is WA,i totally agree with you that i should have used Priority Queue,as it is best choice here and i think there is a bug in judge system because of which i am getting WA on using list instead of priority queue which should not happen in idle case.

i found my bug on my own in my solution,with list also it works,i had mistakenly put v instead of n in outer loop’s condition statement of nested loop.

Thanks for sharing information with us.