1. Resolution
TheAlgorithmIn fact, this is the revision of the breadth-first algorithm, but the general queue in the breadth-first algorithm is changed to the priority queue here.
2. algorithm instance
# Include <iostream> <br/> # include <malloc. h> <br/> # include <queue> <br/> # include <algorithm> <br/> # include <stdlib. h> <br/> # include <functional> <br/> using namespace STD; </P> <p> # define maxnum 100 // defines the maximum number of vertices for the proof of the adjacent vertex <br/> # define maxweight 1000000 // maximum edge weight <br/> // vertex Information <br/> typedef struct <br/> {<br/> int ID; <br/> int Dist; <br/>} node; <br/> // The structure of the graph's Adjacent matrix <br/> typedef struct <br/> {<br/> // char V [maxnum]; // vertex information of the graph <Br/> node V [maxnum]; <br/> int e [maxnum] [maxnum]; // vertex information of the graph <br/> int vnum; // Number of vertices <br/> int Enum; // Number of edges <br/>} graph; <br/> // function declaration <br/> void creategraph (graph * g); // create graph G <br/> int CMP (node A, Node B ); // define whether priority queues are sorted in ascending or descending order <br/> void Dijkstra (graph * g); // The core algorithm is a BFS revision, only the normal queue is changed to the priority queue. <Br/> // define the sorting type. Sort Dist in ascending order of node <br/> int CMP (node A, Node B) <br/>{< br/> return. dist <B. dist; // Ascending Order <br/>}< br/> // Dijkstra algorithm <br/> void Dijkstra (graph * g) <br/>{< br/> node Q [maxnum]; // defines the struct array <br/> int front; // queue header <br/> int rear; // end of the queue <br/> int count; // queue count <br/> front = rear = COUNT = 0; // indicates that the queue is empty <br/> int K, i, J; <br/> // initialize the DIST value <br/> for (I = 1; I <= G-> vnum; I ++) <br/>{< br/> G-> V [I]. dist = maxweight; // dist is the maximum value <br/> G-> V [I]. id = I; <B R/>}< br/> G-> V [1]. dist = 0; // 1 as the source point, DIST is 0 <br/> // The following two rows are elements in the queue operation <br/> q [++ rear] = G-> V [1]; <br/> count ++; // The vertex enters the queue q <br/> while (count> 0) <br/>{< br/> sort (q + front + 1, q + rear + 1, CMP); // sort queue Q, sort by Dist in descending order. <Br/> // The following two rows are queue departure Operations <br/> node n1 = Q [++ front]; <br/> count --; // output queue operation <br/> K = n1.id; // the minimum value in the queue is the shortest path. <br/> for (j = 1; j <= G-> vnum; j ++) <br/>{< br/> If (G-> E [k] [J]! = Maxweight) // K-> edge between j <br/>{< br/> If (G-> V [J]. dist> (G-> V [K]. dist + G-> E [k] [J]) <br/> {<br/> G-> V [J]. dist = G-> V [K]. dist + G-> E [k] [J]; <br/> q [++ rear] = G-> V [J]; <br/> count ++; <br/>}< br/> void creategraph (graph * g) // create graph G <br/>{< br/> cout <"creating undirected graph... "<Endl; <br/> cout <" Enter the number of vertices vnum: "; <br/> CIN> G-> vnum; <br/> int I, j; <br/> // construct an adjacent matrix. The distance from a vertex to itself is infinite. <Br/> cout <"input Matrix Weight:" <Endl; <br/> for (I = 1; I <= G-> vnum; I ++) <br/> for (j = 1; j <= G-> vnum; j ++) <br/>{< br/> CIN> G-> E [I] [J]; <br/> If (G-> E [I] [J] = 0) <br/> G-> E [I] [J] = maxweight; <br/>}</P> <p> int main () <br/>{< br/> graph * g; <br/> G = (graph *) malloc (sizeof (graph); <br/> creategraph (g); <br/> Dijkstra (g ); <br/> cout <"Dijkstra algorithm single-source (source: 1) Shortest Path:" <Endl; <br/> for (int K = 1; k <= G-> vnum; k ++) <br/>{< br/> cout <G-> V [K]. dist <"; <br/>}< br/> cout <Endl; <br/> system (" pause "); <br/> return 0; <br/>}< br/>/* <br/> creating an undirected graph... <br/> enter the number of vertices (vnum: 5) <br/> enter the right of the adjacent matrix: <br/> 0 4 2 0 0 <br/> 0 0 3 2 3 <br/> 0 1 0 4 5 <br/> 0 0 0 0 0 <br/> 0 0 0 1 0 <br/> Dijkstra algorithm single source (source: 1) shortest Path: <br/> 0 3 2 5 6 <br/> press any key to continue... <br/> */