Campus navigation problem (limited to 1 people to complete)
Design Requirements: Design Your school floor plan, including at least more than 10 places, each two places can have a different road, and the length of the road may be different, find out from any place to reach another one of the best path (shortest path).
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map >using namespace std; #define TITLE "Path query system" Const int menu_main_count = 4;const int MAXN = 1001;const int INF = 0x3f3f3f 3f;bool Vis[maxn];int PRE[MAXN], LOWCOST[MAXN], ANS[MAXN], CNT, Q;int cost[maxn][maxn];int N, M, w;string s, T, U, V;MAP&L T;string, Int>mp;map<int, String>mp2;void input_road (); void Show_road (); void Exit_system (); void Dijkstra ( int n, int beg) {for (int i = 0; i < n; i++) {lowcost[i] = INF; Vis[i] = false; Pre[i] =-1; } Lowcost[beg] = 0; for (int j = 0; J < N; j + +) {int k =-1; int Min = INF; for (int i = 0; i < n; i++) {if (!vis[i] && lowcost[i] < min) {min = lowcost[i]; K = i; }} if (k = =-1) break; Vis[k] = true; for (int i = 0; i < n; i++) {if (!vis[i] && lowcost[k] + Cost[k][i] < Lowcost[i]) {lowcost[i] = Lowcost[k] + cost[k][i]; Pre[i] = k; }}}}void init () {memset (cost, 0x3f, sizeof); CNT = 1; q = 0; Mp.clear (); Mp2.clear ();} void Add (String u) {if (!mp[u]) {mp[u] = cnt; MP2[CNT] = u; cnt++; }}void dfs (int u) {if (U = =-1) return; ans[q++] = u; DFS (Pre[u]);} void Print_menu_title (Char*title) {printf ("=================================================\n"); printf ("| %s\n ", title); printf ("-------------------------------------------------\ n");} Char menu_main[]= "|\n" | 1 input path \ n "" | 2 query path \ n "" | 3 exit \ n "" |\n "; void (*menu_main_func[]) () ={input_road, Show_road, Exit_system};void Print_menu_main () {in T selected=0; System ("CLS"); Print_menu_title (title); printf (Menu_main); printf ("=================================================\n"); while (!) ( Selected>=1&&selected<=meNu_main_count) {printf ("> Please select:"); scanf ("%d", &selected); if (Selected>=1&&selected<=menu_main_count) {break; } printf ("\n> input Error!" Please select 1-%d) \ n ", Menu_main_count); } Menu_main_func[selected-1] ();} void Input_road () {cout << | Enter the number of buildings "<<" << "Number of input roads" << Endl; CIN >> n >> m; Init (); cout << "| Input starting point "<< Endl; Cin >> S; Add (s); cout << "| Input end point "<< Endl; CIN >> T; Add (t); cout << "| Output "<< m <<" two endpoints and lengths of the sidebar "<< Endl; for (int i = 0; i < m; i++) {cin >> u >> v >> w; Add (u); Add (v); COST[MP[U]][MP[V]] = min (Cost[mp[u]][mp[v]], W); Cost[mp[v]][mp[u]] = cost[mp[u]][mp[v]]; } print_menu_main ();} void Show_road () {Dijkstra (n+1, Mp[s]); cout << "| Shortest path length: "<< lowcost[mp[t]" << Endl; DFS (mp[t]); cout << "| Route: "<<" "<< mp2[ans[q-1]"; for (int i = q-2; I >= 0; i--) cout << "--" << mp2[ans[i]]; cout << Endl; GetChar (); GetChar (); cout << "Press any key to exit" << Endl; Print_menu_main ();} void Exit_system () {exit (0);} int main () {print_menu_main (); return 0;}
Data structure Operation Campus navigation problem