1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
#include #include #include USINGNAMESPACESTD; /** * Author: Dmego * Time: 2016-12-12 */ #define MAX 1000000//represents maximum ∞ #define MAX 10 bools[max];//record from source point V0 to Endpoint VI is determined to be the shortest path, OK to remember true, or false intpath[max];//records the direct precursor vertex number of Endpoint VI on the current shortest path from source point V0 to Endpoint VI, if V0 to VI has edge precursor to V0 otherwise 1 intd[max];//record the length of the shortest path between the source point and the endpoint, the right value of the side that V0 to VI, otherwise recorded as Max Typedefstruct { String vexs[max];//vertex table intarcs[max][max];//adjacency Matrix Intvexnum, arcnum;//current points and number of edges }amgraph; Using Dijkstra algorithm to find shortest path Voidshortestpath_dij (Amgraph &g,intv0) {////using the Dijkstra algorithm to find the shortest path of the V0 point to the remaining vertices in the network g INTN = g.vexnum;//Top points for (Intv = 0; v < n; v++)//n vertices are initialized sequentially { S[V] =false;//s initialized to empty set D[V] = g.arcs[v0][v];//initializes the shortest path length v0 to each endpoint to a weighted value on the edge if (D[v] < MAX) PATH[V] = v0;//If there is an edge between V0 and V, the predecessor of V is initialized to V0 Else PATH[V] = -1;//if V0 and V are boundless, initialize V's predecessor to-1 } S[v0] =true;//adds V0 to S D[v0] = 0;//source point to Source point has a weight value of 0 ---------initialization ends, start the main loop, and each time the shortest path to a vertex is v0, the V is added to the S array for (inti = 1; i < n; i++)//To compute the remaining n-1 vertices in turn { Intmin = MAX; Intv = V0; for (INTW = 0; w < n; w++) { if (! S[W] && d[w] < min) {//Select a current shortest path with end point V v = w; min = d[w]; } S[V] =true;//adds v to the S set for (INTW = 0; w < n; w++) {//update the shortest path length from v0 to all vertices on the v-s set 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 the predecessor of W to V } } } } } Background function Voidbackground () { cout << "|*****************************************************************| 100= "" 125= "" 135= "" 140= "" 145= "" 150= "" 160= "" 200= "" 230= "" cout= ""------= "" >>> Please select: "; } Attractions Information Inquiry level two menu Voidjmenu () { cout << "|*****************************************************************| cout= "" >>> please check the number of attractions: "; } Shortest Path Query level two menu Voidpmenu () { cout << "|*****************************************************************| cout= "" >>> Please enter the start number and the end number in sequence: "; } Voidmain () { Initialization operation Amgraph AMG = {"Information college", "comprehensive Restaurant", "West Playground", "Gymnasium", "Chunhui House", "The Foundation teaches", "Nine teaches", "Nine Buildings", "Qin Yuan", "Cui Yuan"}, -1 represents the two sides are not connected, the weight of infinity Adjacency Matrix/* Letter Chunchi {{max,max,max,140,max,200,150,max,100,125}, {Max,max,max,max,145,230,max,100,max,max}, {Max,max,max,150,max,max,200,max,max,max}, {140,max,150,max,max,max,max,max,100,max}, {Max,145,max,max,max,150,max,max,max,max}, {200,230,max,max,150,max,max,160,max,135}, {150,max,200,max,max,max,max,max,max,max}, {Max,200,max,max,max,160,max,max,max,max}, {100,max,max,100,max,max,max,max,max,max}, {125,max,max,max,max,135,max,max,max,max} },10,14}; INTF, FF; Intstart, end; while (true) { cout << Endl; menu (); Cin >> F; if (f = = 1) { JMenu (); CIN >> FF; Attraction information read from the file Ifstream outfile ("Schooltravel.txt", iOS:: Out | iOS:: binary); if (!outfile) { Cerr << "Read the introduction file of the attraction failed!" string= "" inti= "M-1" cout= "" ff= "=" f= "=" cin= "" start= "" = "" inttemp= "End-1;</div" m= "0;</div" end= "" temp1= "tem P "temp2=" Path[temp1];</div "temp=" Temp2;</div "i=" ">"; } cout << Endl; cout << "Shortest Path value is: end=" "=" "f=" = "cout=" >>> exit successful! "<< Endl; Exit (0); } } } |