资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
数据结构实验报告交通指南系统题目:假设以一个带权有向图表示某一区域的公交线路网,图中顶点代表一些区域中的重要站点,弧长代表已有的公交线路,弧上的权表示该线路上的票价(或搭乘所需时间),试设计一个交通指南系统,指导前来咨询者以最低的票价或最少的时间从区域中的某一站点到达另一站点。#include using namespace std;struct ArcCellint adj; /存放弧长 bool *info; /是否用过该弧;struct _MGraph char vexs20; /存放站点ArcCell arcs2020; /int vexnum;int arcnum;typedef int Path202020; typedef int Distanc2020; class MGraph /没用私有成员 public:_MGraph mgraph;/void DestroyGraph(); /析构函数销毁图int LocateVex (char u); / 返回顶点在图中的位置bool CreateDN(); /构造有向网void ShortestPath_FLOYD(Path &P,Distanc &D);bool MGraph:CreateDN()/构造有向网int i,j ,w;char v1, v2;coutmgraph.vexnummgraph.arcnum ;coutn请输入各站点名: ;for(i = 0;imgraph.vexsi;for(i = 0;imgraph.vexnum;i+)/初始化邻接矩阵for(j = 0;jmgraph.vexnum;j+)if(i=j)mgraph.arcsij.adj = 0;elsemgraph.arcsij.adj = 20000; /infinity; mgraph.arcsij.info = false;for(i = 0;imgraph.arcnum;i+) /构造邻接矩阵coutv1v2w;int m = LocateVex(v1);int n = LocateVex(v2);mgraph.arcsmn.adj = w; / 的权值return true; void MGraph:DestroyGraph()for(int i = 0 ;imgraph.vexnum;i+)for(int j = 0;jmgraph.vexnum;j+)if(mgraph.arcsij.info)delete mgraph.arcsij.info;mgraph.arcsij.info = false;mgraph.vexnum = 0;mgraph.arcnum = 0;int MGraph:LocateVex(char u)for(int i = 0 ;i20;i+)if(u = mgraph.vexsi)return i;return -1;void MGraph:ShortestPath_FLOYD(Path &P,Distanc &D)/求每对顶点间的最短路径/ 用Floyd算法求有向网G中各对顶点v和w之间的最短路径Pvw及其带权长度Dvw/ 若Pvwu为TRUE,则u是从v到w当前求得最短路径上的顶点。 int u,v,w,i;for(v = 0;vmgraph.vexnum;v+)for(w = 0;wmgraph.vexnum;w+)Dvw = mgraph.arcsvw.adj;/ 顶点v到顶点w的直接距离for(u = 0;umgraph.vexnum;u+)Pvwu = false; /路径矩阵初值if(Dvw20000) /从v到w有直接路径Pvwv = Pvww = true;/由v到w的路径经过v和w两点for(u = 0;umgraph.vexnum;u+)for(v = 0;vmgraph.vexnum;v+)for(w = 0;wmgraph.vexnum;w+)if(Dvu+DuwDvw)/从v经u到w的一条路径更短Dvw = Dvu+Duw;/ 更新最短距离for(i = 0;imgraph.vexnum;i+) Pvwi = Pvui|Puwi;/从v到w的路径经过从v到u和从u到w的所有路径void main()MGraph g;Path p; / 3维数组Distanc d; / 2维数组int s,t,k;char v1,v2;float sum=0;coutn*欢迎使用交通查询系统*nendl;g.CreateDN();coutv1v2;s=g.LocateVex (v1);t=g.LocateVex (v2); g.ShortestPath_FLOYD(p,d); if(s!=t) int a=s;coutn由g.mgraph.vexss到g.mgraph.vexst途中经过:;for(k = 0;kg.mgraph.vexnum;k+)if(pstk = 1)coutg.mgraph.vexsk ;sum=sum+g.mgraph.arcsak.adj;a=k;cout最短线路总长:sum公里;coutnn*祝您旅途愉快!*endl;g.DestroyGraph();
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号