Single source is the shortest circuit, the complexity is O (N²), and the heap is optimized O (NLOGN). The basic idea is greed, each time to join a current closest point, can prove each time the nearest point is the shortest path at the moment. Therefore, when all points are added, the shortest path from the starting point to all points is calculated.
In the implementation, it should be noted that in the heap of a point I, not as long as the shortest path length of the current to I D[i], but also remember that this is the length of the I point . In other words, be able to know at any time the corresponding value of point d[i] in the heap position, and a point in the heap h[k] corresponding to the point I. My approach is to use two arrays in the heap to maintain this one by one corresponding relationship.
Here's the pseudo-code for the subject:
1 d←∞, Heap h is empty2d[s]=03 for(A bit i) H.inch(I,d[i]);4 while(heap non-empty)5 {6U=h. out();7 for(All sides u->v)8 if(d[v]>d[u]+(u,v))9 {Tend[v]=d[u]+(u,v); One h.update (V,d[v]); A } -}
Finally, the Dijkstra algorithm, not only can not appear to let the shortest path disappears negative right ring, also cannot appear negative right side. Because it is greedy, once the negative right side, then may go to a longer side, and then through the negative right side to come back, to achieve a shorter path, so Dijkstra greedy ideas will expire.
Heap optimization will not be written, there is time to add it.
"Algorithm" C + + code Dijkstra