Dijstra algorithm code for reference:
#include <cstdio> #include <CString> #include <algorithm> #include <the iostream> #define Inf 0x3f3f3f3f the using namespace STD; int Map [ 1005 ] [ 1005 ]; // store input array values int VIS [ 1005 ], DIS [ 1005 ]; // VIS tag array, dis shortest path int n-, m; // n-points, m edges void the Init () { Memset (map, Inf, the sizeof (map)); // initialize array map for ( int I = . 1; I <= n-; I ++ ) { Map [I] [I] = 0 ; } // the right node to the node value is set to 0, i.e. the value set on the array of slash 0 } void GetMap () { int U, V, W; for ( int T = . 1 ; T <= m; T ++ ) { Scanf ( " % D% D% D " , & U, & V, & W); // enter the array ranks subscript for the point weight IF (Map [U] [V]> W) // If initialization Map [] [] array values (init function is set to infinity) is greater than the priority value of current input { Map [U] [V] = W; Map [V] [U] = W; } } } Void the Dijkstra ( int U) // for nodes operating { Memset (VIS, 0 , the sizeof (VIS)); // VIS empty array for ( int T = . 1 ; T <= n-; T ++ ) { DIS [T] = map [U] [T]; // start value stored from the first row map to the dis array, dis array storing shortest path weight } vis [U] = . 1 ; // the array opposed vis n is a node weight marker array, the representative has accessed // double loop looking for the shortest path for ( int T = 1 ; T <n; T ++ ) { Int Minn = Inf, TEMP; // define the variable min is infinite for ( int I = . 1 ; I <= n-; I ++ ) { IF ! (VIS [I] && DIS [I] <Minn) // not visited the node and storing shortest path dis array at this point is less than the weight minn { minn = dis [I]; // to dis array weights to the point value update stored minn variable TEMP = I; // update the array subscript } } VIS [TEMP] = . 1 ; // tag has visited the site for ( int I = . 1 ; I <= n-; I ++ ) { IF(Map [TEMP] [I] + DIS [TEMP] <DIS [I]) // weight if the current node + current stored in the right right shortest path node and a cumulative value less than the previous node values { DIS [I] = Map [TEMP] [I] + DIS [TEMP]; // weight before updating a node } } } } int main () { Scanf ( " % D% D " , & m, & n-); the Init (); GetMap (); the Dijkstra (n-); the printf ( " % D \ n- " , DIS [ 1 ]); // print array subscript 1 to the other single-source shortest path point return 0 ; }
The main idea of Dijkstra's algorithm:
map initialize the array, vis tag array, dis storing shortest path length
Core code:
for ( int T = . 1 ; T <n-; T ++ ) { int Minn = Inf, TEMP; // define the variable min is infinite for ( int I = . 1 ; I <= n-; I ++ ) { IF ! (VIS [I] dis && [I] <Minn) // not accessed the node and storing shortest path dis array at this point is less than the weight Minn { Minn = dis [I]; // to dis array weights to the point value update minn stored variable TEMP = I; // update array subscript } } VIS [TEMP] = . 1 ; // tag has visited the site for (int I = . 1 ; I <= n-; I ++ ) { IF (Map [TEMP] [I] + DIS [TEMP] <DIS [I]) // weight if the current node + storing shortest path current node weights and accumulated point value is less than the previous node weights { DIS [I] = Map [TEMP] [I] + DIS [TEMP]; // update right before a value of a node } } }
Similar prime algorithm, the object matter Dijkstra algorithm is a directed graph or undirected graph, it can find a single source shortest path (a shortest path to the rest of the point to point), time complexity is O (n * n).
the difference:
The core idea is updating the connection path, Prime is tracked to the next node in the minimum spanning tree edges intersect, but are dijkstra track to the next node
Path through the starting point for all nodes and.