The previous introduction of "native Dijkstra" is not optimized Dijkstra, but this dijkstra efficiency is n^n, so in the face of large data volume need to optimize it, that is, optimize the use of greedy strategy implementation, so there is heao+ Dijkstra heap Optimization Dijkstra, but the implementation of heap optimization is very complex, and Priorityqueue+dijkstra priority queue optimization Dijstra efficiency is slightly lower than the Dijkstra of the heap optimization, but the implementation is much easier, is not prone to error, Because the Java class Library can be implemented with priorityqueue, so the priority queue optimization Dijkstra is preferred, in fact, Java class Library priorityqueue the underlying implementation principle is to push the sorting.
Also take the Blue Bridge Cup "shortest path" as an example to achieve Priorityqueue+dijkstra:
algorithm Training Shortest Pathtime limit: 1.0s memory limit: 256.0MBProblem Description
Given an n vertex, the forward graph of the M-Edge (some of which may be negative, but no negative ring is guaranteed). Please calculate the shortest path from point 1th to other points (vertices are numbered from 1 to n).
Input Format
First line two integers n, M.
The next M-line, each line has three integers u, V, L, indicating that u to V has an edge of length L.
output FormatA total of n-1 lines, line I represents the shortest path from point No. 1th to I+1. Sample Input3 3
1 2-1
2 3-1
3 1 2Sample Output-1
-2data size and conventions
For 10% of data, n = 2,m = 2.
For 30% of data, n <= 5,m <= 10.
For 100% of data, 1 <= n <= 20000,1 <= m <= 200000,-10000 <= L <= 10000, guaranteeing that all vertices can be reached from any vertex.
The Priorityqueue+dijkstra implementation of Priorityqueue based on Java class Library:
<span style= "Font-family:microsoft yahei;font-size:14px;" ><span style= "Font-family:microsoft yahei;font-size:14px;" >import java.util.hashmap;import java.util.hashset;import Java.util.iterator;import java.util.PriorityQueue; Import Java.util.scanner;import java.util.set;/** * priorityqueue + Dijkstra algorithm for single source shortest path * First This method * Although Priority queue optimization is less than heap optimization performance, the gap is very small. * But the priority queue can be implemented directly using the Priorityqueue in ADT, while the heap optimization implementation is very complex. * * @author Duxiangyu * */public class Dijkstra_link_queue {static int nodecount;static int edgecount;//adjacency table header array static N Ode[] firstarray;//Shortest path array//static int[] dist;//s Collection, representing the node where the shortest path has been found static hashset<integer> s;//map set static dist[] distarray;//priority queue static priorityqueue<dist> pq;static int max = 1000000;/** * Node class * * @author Duxiangyu */static C Lass Node {//adjacency vertex mapprivate hashmap<integer, integer> map = null;public void Addedge (int end, int edge) {if (this.m AP = = null) {This.map = new Hashmap<integer, integer> ();} This.map.put (end, Edge);}} /** * Dist: Save source nodeShortest path to each node * @author Duxiangyu * */static class Dist implements comparable<dist> {int value;int index;public dist (in t value, int index) {this.value = Value;this.index = index;} @Overridepublic int compareTo (dist o) {if (O.value < This.value) {return 1;} else if (O.value > This.value) {return -1;} else {return 0;}}} public static void Main (string[] args) {Scanner sc = new Scanner (system.in); nodecount = Sc.nextint (); edgecount = Sc.nextin T (); firstarray = new Node[nodecount + 1];for (int i = 0; i < Nodecount + 1; i++) {Firstarray[i] = new Node (); for (int i = 0; i < Edgecount; i++) {int begin = Sc.nextint (); int end = Sc.nextint (); int edge = Sc.nextint (); Firstarray [Begin].addedge (end, Edge);} Sc.close (); Long begin = System.currenttimemillis ();d jst (); Long end = System.currenttimemillis (); System.out.println (End-begin + "MS");} /** * Heap + Dijkstra algorithm implemented */private static void Djst () {s = new hashset<integer> ();p q = new Priorityqueue<dist> ;(nodecount);d Istarray = new DIst[nodecount + 1]; Node Tempnode = firstarray[1];for (int i = 2; i < Nodecount + 1; i + +) {Hashmap<integer, integer> tempmap = TEMPN Ode.map;if (Tempmap.containskey (i)) {dist d = new Dist (Tempmap.get (i), i);p Q.offer (d);d istarray[i] = D;} else {dist d = NE W Dist (max, i);p Q.offer (d);d istarray[i] = D;}} S.add (1); while (S.size () < Nodecount) {dist d = pq.poll (); int index = d.index;int value = D.value;s.add (index);//With indx This point goes to update its adjacency point to the starting point of the distance hashmap<integer, integer> m = firstarray[index].map;if (M = = null) {continue;} set<integer> set = M.keyset (); iterator<integer>it = Set.iterator (); while (It.hasnext ()) {int num = It.next () if (num = = 1) {continue;} Dist Tempdist = distarray[num];if (M.get (num) + value < Tempdist.value) {pq.remove (tempdist); tempdist.value = M.get (nu m) + value;pq.offer (tempdist);d istarray[num] = Tempdist;}}} for (int i = 2; i < Nodecount + 1; i + +) {System.out.println (Distarray[i].value);}}} </span></span>
Performance test:
Or for the 10,000-node and 100,000-edge test data, the average execution time of the priority queue optimization is 320ms, which is slightly lower than the execution efficiency of the heap optimization, but the relative native Dijkstra elevation is quite obvious:
Dijstra of Priorityqueue+dijkstra Priority queue optimization