Can anybody please help me with this question.I know how to calculate shortest path but I have no idea how to actually trace the nodes in the shortest path.What modification to naive dijktra's has be made?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
8 | Dominater069 | 154 |
8 | nor | 154 |
Can anybody please help me with this question.I know how to calculate shortest path but I have no idea how to actually trace the nodes in the shortest path.What modification to naive dijktra's has be made?
Name |
---|
When using JAVA, I'd create a class called Node, and have a String attribute for the path. Every node I enqueue in the priority queue, I'd the the node it's coming through + the previous path.
Maybe there is a better way, but that's how I do it.
Keep another array 'B', if you are currently in node 'v', when you want to update the shortest distance of a node 'u', write 'B[u]=v' , finally, to get the shortest path, start from the ending node, and go backwards in the array B until you reach the starting node :
now you have the path (backwards)
I applied your logic but i am not getting correct answer maybe anything wrong with concepts.Can you see this what it wrong with my implementation http://ideone.com/d47g3K
Line 37, instead of
if(!f[v] && d[s]+w<d[v])
It should be
if(d[s]+w<d[v])
Sir,if you won't mind can I ask you the logic behind removing the !f[v].
Im sorry, that wasn't the reason for the bug, this is:
when you have a priority_queue of pairs, it will sort according to first element, then according to the second.
So you should swap between first and second (first is length, second is node)
AC Code : 11353425
What I did was maintain an array par[] and for each node,i, explored store its parent vertex in par[i]. Then to output shortest path, start from n and keep printing par[] values till par[i] is equal to start vertex.