1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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
| #include"MGraph.h"
Status DijsMinPath(MGraph G, int v0); Status DispDijsPaths(MGraph G, int *D, int *fa, bool *final, int v0); Status MinDijsPrint(MGraph G, int *fa, int v, int v0);
Status FloydMinPath(MGraph G); Status MinFloydPrint(MGraph G, int *path, int v, int w); Status DispFloydPaths(MGraph G, int *D, int *path); int main() { MGraph G; CreateGraph(G); GraphPrint(G); cout << "----------------------Dijkstra MinPath Problem Page------------------\n"; cout << "Input the source node: "; VertexType sv; cin >> sv; int v0 = LocateVex(G, sv); DijsMinPath(G, v0); cout << "-----------------------Floyd MinPath Problem Page---------------------\n"; FloydMinPath(G); return 0; }
Status DijsMinPath(MGraph G, int v0) { int fa[Max_Vertex_Num], D[Max_Vertex_Num]; bool final[Max_Vertex_Num]; int v = 0, w = 0, i = 0; inp(v,0, G.vexnum-1){ final[v] = false; D[v] = G.arcs[v0][v].adj; if(D[v] != INFINITY) fa[v] = v0; }
final[v0] = true; inp(i,1,G.vexnum-1){ int min_path = INFINITY; inp(w,0,G.vexnum-1){ if(!final[w]) if(D[w] < min_path){ v = w; min_path = D[w]; } } if(min_path != INFINITY) final[v] = true; else break; inp(w,0,G.vexnum-1){ if(!final[w] && G.arcs[v][w].adj != INFINITY) if(min_path + G.arcs[v][w].adj < D[w]){ D[w] = min_path + G.arcs[v][w].adj; fa[w] = v; } } } DispDijsPaths(G, D, fa, final, v0); return true; } Status DispDijsPaths(MGraph G, int *D, int *fa, bool *final, int v0) { int i = 0; inp(i, 0, G.vexnum-1){ if(i != v0){ if(final[i]){ cout << "from " << G.vexs[v0] << " to " << G.vexs[i] << " the shortest path length: " << D[i]; cout << endl << "Display the path:\n" << G.vexs[v0] << "->"; MinDijsPrint(G, fa, i, v0); cout << G.vexs[i] << endl; } else cout << "from " << G.vexs[v0] << " to " << G.vexs[i] << " there is no road.\n"; } } return true; } Status MinDijsPrint(MGraph G, int *fa, int v, int v0) { int w = fa[v]; if(w == v0){ return true; } MinDijsPrint(G, fa, w, v0); cout << G.vexs[w] << "->"; return true; }
Status FloydMinPath(MGraph G) { int path[G.vexnum][G.vexnum], D[G.vexnum][G.vexnum]; int i = 0, j = 0, k = 0; inp(i, 0, G.vexnum-1) inp(j, 0, G.vexnum-1){ D[i][j] = G.arcs[i][j].adj; if(D[i][j] != INFINITY) path[i][j] = i; else path[i][j] = -1; } inp(k, 0, G.vexnum-1) inp(i, 0, G.vexnum-1) inp(j, 0, G.vexnum-1) if(D[i][k] != INFINITY && D[k][j] != INFINITY) if(D[i][j] > D[i][k] + D[k][j]){ D[i][j] = D[i][k] + D[k][j]; path[i][j] = path[k][j]; } DispFloydPaths(G, &D[0][0], &path[0][0]); return true; } Status DispFloydPaths(MGraph G, int *D, int *path) { int i = 0, j = 0, k = 0; inp(i,0,G.vexnum-1){ cout << "-------------------------------------------------------------\n"; inp(j,0,G.vexnum-1){ if(i == j) continue; if(D[i*G.vexnum + j] != INFINITY){ cout << "from " << G.vexs[i] << " to " << G.vexs[j] << " the shortest path length: " << D[i*G.vexnum + j] << endl; cout << "Display the path:\n" << G.vexs[i] << "->"; MinFloydPrint(G, path, i, j); cout << G.vexs[j] << endl; } else cout << "from " << G.vexs[i] << " to " << G.vexs[j] << " there is no road.\n"; } cout << "-------------------------------------------------------------\n"; } return true; } Status MinFloydPrint(MGraph G, int *path, int v, int w) { int p = path[v*G.vexnum + w]; if(p == v){ return true; } MinFloydPrint(G, path, v, p); cout << G.vexs[p] << "->"; return true; }
|