南郵數(shù)據(jù)結(jié)構(gòu)實驗三.doc_第1頁
南郵數(shù)據(jù)結(jié)構(gòu)實驗三.doc_第2頁
南郵數(shù)據(jù)結(jié)構(gòu)實驗三.doc_第3頁
南郵數(shù)據(jù)結(jié)構(gòu)實驗三.doc_第4頁
南郵數(shù)據(jù)結(jié)構(gòu)實驗三.doc_第5頁
免費預覽已結(jié)束,剩余4頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

實 驗 報 告(2015 / 2016 學年 第 一 學期)課程名稱數(shù)據(jù)結(jié)構(gòu)實驗名稱圖的基本運算及飛機換乘次數(shù)最少問題實驗時間2015年12月4日指導單位計算機科學與技術(shù)系指導教師黃海平學生姓名陳明陽班級學號Q14010119學院(系)貝爾英才專 業(yè)信息科技強化班實 驗 報 告實驗名稱圖的基本運算及飛機換乘次數(shù)最少問題指導教師黃海平實驗類型驗證實驗學時4實驗時間12.4一、 實驗目的和要求飛機最少換乘次數(shù)問題。(1)設有n個城市,編號為0n-1,m條航線的起點和終點由用戶輸入提供。尋找一條換乘次數(shù)最少的線路方案。(2)參考:可以使用有向圖表示城市間的航線;只要兩城市間有航班,則圖中這兩點間存在一條權(quán)值為1的邊;可以使用Dijkstra算法實現(xiàn)。二、實驗環(huán)境(實驗設備)VSUAL STUDIO2015三、實驗原理及內(nèi)容對象視圖:源代碼:Graph.h#include #includeusing namespace std;const int INF = 2147483647;enum ResultCode Underflow, Duplicate, Failure, Success, NotPresent, OutOfBounds ;template class Graph /抽象類 public:virtual ResultCode Insert(int u, int v, T w) = 0;virtual ResultCode Remove(int u, int v) = 0;virtual bool Exist(int u, int v)const = 0;protected:int n, e;template class MGraph :public Graph /鄰接矩陣類 public: MGraph(int mSize, const T noedg); MGraph(); ResultCode Insert(int u, int v, T w); ResultCode Remove(int u, int v); bool Exist(int u, int v)const; int Choose(int *d, bool *s); void Dijkstra(int v, T *d, int *path);protected: T *a; T noEdge;template MGraph:MGraph(int mSize, const T noedg) n = mSize; e = 0; noEdge = noedg; a = new T*n; for (int i = 0; in; i+) ai = new Tn; for (int j = 0; jn; j+) aij = noEdge; aii = 0; template MGraph:MGraph() for (int i = 0; in; i+) deleteai; deletea;template ResultCode MGraph:Insert(int u, int v, T w) if (u0 | vn - 1 | vn - 1 | u = v) return Failure; if (auv != noEdge) return Duplicate; auv = w; avu = w; e+; return Success;template ResultCode MGraph:Remove(int u, int v) if (u0 | vn - 1 | vn - 1 | u = v) return Failure; if (auv = noEdge) return NotPresent; auv = noEdge;avu = noEdge; e-; return Success;templatebool MGraph:Exist(int u, int v)const if (u0 | vn - 1 | vn - 1 | u = v | auv = noEdge) return false; return true;template int MGraph:Choose(int *d, bool *s) /求最小di int i, minpos; T min; min = INF; minpos = -1; for (i = 0; in; i+) if (di = min&!si) min = di; minpos = i; return minpos;template void MGraph:Dijkstra(int v, T *d, int *path) /迪杰斯特拉算法 int i, k, w; if (vn - 1) throw OutOfBounds; bool *s = new booln; for (i = 0; in; i+) si = false; di = avi; if (i != v&diINF) pathi = v; else pathi = -1; sv = true; dv = 0; for (i = 1; in; i+) k = Choose(d, s); sk = true; for (w = 0; wn; w+) if (!sw & (dk + akw)dw) dw = dk + akw; pathw = k; 源.cpp#include #include#includeGraph.husing namespace std;int main() int n, m; cout n; cout m; MGraphA(n, INF); int c, f; cout 請輸入每條航線的起點和終點: endl; for (int i = 0; im; i+) cout 航線 i + 1 c f; A.Insert(c, f, 1); char s; do int v, w; cout v w; while (v0 | wn - 1 | vn - 1) cout v w; int *b = new intn; int *d = new intn; int *path = new intn; A.Dijkstra(v, d, path); int e = n - 1;int j, i,k = 0; for (j = 0; jn; j+) bj = -2; if (w != v) j = w; while (pathj != -1) be = pathj; e-; j = pathj; if (e = n - 1 | dj = INF) cout 這兩點間無線路! endl; else cout 從 v 城市到 w 城市的換乘次數(shù)最小的線路方案為:; for (k = 0; kn; k+) if (bk != -2) cout bk ,; cout w endl; if (w = v) cout 看來你錢比較多 endl; deleteb; deleted; deletepath; cout s; while (s != Y&s != y&s != n&s != N) cout s; while (s = Y | s = y); return 0;運行截圖:實 驗 報 告四、實驗小結(jié)(包括問題和解決方法、心得體會、意見與建議等)使用鄰接表表示的圖和迪杰斯特拉算法實現(xiàn)飛機最小換乘路徑本來不算一件難事,這個程序?qū)崿F(xiàn)起來的重點在于容錯性的問題。在考慮到輸入時可能有的各種各樣問題時主程序中的輸入判斷就要多改進。還有就是我怎么都覺得飛機路線圖怎么

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論