Shortest path-dijkstra detailed-source code

Source: Internet
Author: User

Tag:dijkstra    algorithm     source code    

Algorithm 6.10 Dijkstra algorithm # include <iostream>using namespace std, #define MAXINT 32767//Represents the maximum value, i.e. ∞ #define MVN              UM 100//MAX vertex number typedef char Vertextype;                  Assume that the data type of the vertex is a character typedef int arctype;                    Assume that the weight of the edge type is int *d=new int[mvnum];          Used to record the shortest short length of bool *s=new bool[mvnum]; Marks whether the vertex enters the S-set int *path=new int[mvnum];//the adjacency matrix of the precursor//------------graph used to record the shortest-path vertex-----------------typedef struct{            Vertextype Vexs[mvnum];      Vertex table Arctype Arcs[mvnum][mvnum];                adjacency Matrix int vexnum,arcnum; The current number of points and sides of the graph}amgraph;int Locatevex (amgraph G, Vertextype v) {//determines the position of Point V in G for (int i = 0; i < g.vexnum; ++i) if (G.vexs[i]   = = v) return i; return-1;}    Locatevexvoid Createudn (amgraph &g) {//using adjacency matrix notation, creating a non-net G int i, J, k;cout << "Please enter the total number of vertices, total number of edges, separated by spaces:";    CIN >> g.vexnum >> g.arcnum;//input total number of vertices, total number of sides cout << endl;cout << "input point name:, such as a" << Endl; for (i = 0; i < G.vexnum; ++i) {cout << "Please enter the name of" << (i+1) << "dot:"; cin >> G.vexs[i];    Input point information}cout << Endl;  for (i = 0; i < g.vexnum; ++i)//Initialize adjacency matrix, the weight of the edge is set to maximum value maxint for (j = 0; j < G.vexnum; ++j) G.arcs[i][j] = MaxInt; cout << "input edge-dependent vertices and weights, such as a B 7" << endl;for (k = 0; k < g.arcnum;++k) {//constructs adjacency matrix Vertextype v1, v2; Arctype w;cout << "Please enter the section" << (k + 1) << "vertices and weights attached to the edge:"; Cin >> v1 >> v2 >> w;//Enter a vertex attached to an edge  And the weighted value i = Locatevex (G, v1); j = Locatevex (G, v2);//determines the position of V1 and V2 in G, that is, the subscript of the vertex array g.arcs[i][j] = w;//Edge <v1, V2> 's weight is set to W g.arcs[j][i] = g.arcs[i][j];// LT;V1, v2> symmetrical edge <v2, v1> with a weight of W}//for}//createudnvoid shortestpath_dij (amgraph G, int v0) {//Dijkstra algorithm for the net g of V0                    The shortest path from vertex to remaining vertex int V, I, W, min;int n = g.vexnum;                  n is the number of vertices in g for (v = 0; v < n; ++v) {//n vertices are initialized sequentially s[v] = false;           S initial empty set d[v] = G.arcs[v0][v]; The shortest path length v0 to each endpoint is initialized to the weighted if on the arc (d[v] < MaxInt) Path [v] = v0;               If there is an arc between V0 and V, the predecessor of V is set to v0 else Path [v] =-1;                    If there is no arc between V0 and V, the predecessor of V is set to 1}//for s[v0]=true;                      Add V0 to S d[v0]=0;         The distance from the source point to the source point is 0/*― initialization ends, the main loop starts, each time the shortest path v0 to a vertex v is evaluated, the V is added to the S set ―*/for (i = 1;i < n; ++i) {//to the remaining n-1 vertices, and then the min= MaxInt is calculated in turn; for (w = 0; w < n; ++w) if (! S[W] && d[w] < min) {//Select a current shortest path with a terminal point of V v = w; min = d[w];}                   if s[v]=true; Add V to s for (w = 0;w < n; ++w)//update from V0 to set v? The shortest path length of all vertices on s if (!   S[W] && (D[v] + g.arcs[v][w] < D[w])) {d[w] = D[v] + g.arcs[v][w];              Update D[w] Path [w] = V;  Change W's precursor to V}//if}//for}//shortestpath_dijvoid displaypath (amgraph G, int begin, int temp) {//Show Shortest path if (path[temp]! = -1) {Displaypath (G, Begin, Path[temp]); cout << G.vexs[path[temp]] << "--";}} Displaypathvoid Main () {cout << "************ algorithm 6.10 Dijkstra algorithm **************" << Endl << Endl; Amgraph G; int I, J, nUm_start, num_destination; Vertextype start, destination; Createudn (g); cout <<endl;cout << "* * * * * No to NET G create complete! "<< endl;for (i = 0; i < G.vexnum; ++i) {for (j = 0; j < G.vexnum; ++j) {if (j! = g.vexnum-1) {if (g.arcs[i ][J]! = MaxInt) cout << g.arcs[i][j] << "\ t", Elsecout << "∞" << "\ T";} Else{if (g.arcs[i][j]! = MaxInt) cout << g.arcs[i][j] <<endl;elsecout << "∞" &LT;&LT;ENDL;}} Forcout << endl;cout << "Please enter start point, end name:"; Cin >> start >> Destination;num_start = Locatevex (G, S Tart); num_destination = Locatevex (G, destination); Shortestpath_dij (g, Num_start), cout << endl << "Shortest path:";D Isplaypath (g, Num_start, num_destination); Cout & lt;< G.vexs[num_destination]<<endl;} Main

Shortest path-dijkstra detailed-source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.