问答题

【说明】[程序6说明]单源最短路径的分支限界算法。 const int MAXNUM=29999; #include<iostream> #include<vector> #include<algorithm> #include<functional> using namespace std; template <class VertexType,class EdgeType> class MinNode { //程序中使用的最小化堆的结点说明 friend class Graph<VertexType,EdgeType> public: MinNode (int nl, EdgeType length1) { VexNum=nl; length=length1; } bool operator>(const MinNode<VertexType,EdgeType>&p)const { return (1) >p.length; } private: int VexNum; //记录源点序号,序号数组p及distance下标相一致。源点为初始扩展顶点 EdgeType length; //记录源点到本顶点的当前最短路径的长度,源点到自身的长度为0 } template<class VertexType,classEdgeType> void Graph<VertexType,EdgeType>:: shortestpath(VertexType start) { int j,k,source;//source 记录源点的序号。 EdgeType*distance= (2) ; int*p=new int[MaxNumVertex]; vector<MinNode<VertexType,EdgeType> >H; for(source=0;source<MaxNumVertex;source++) { if(NodeList[source]==start)break;} if (source>=MaxNumVertex){cout<<”This is error!”<<end1;return;} MinNode<VertexType,Edge Type> (3) ; for(k=0;k<MaxNumVertex;k++) { distance[k]:MAXXUM; //记录源点到本顶点k的最终的最短路径的长度 p[k]=source; //记录最短路径上的本顶点的直接前驱顶点的序号 } distance[source]=0;p[source]=-1;//m 是源点,前一顶点不存在 vector<MinNode<VertexType, EdgeType>>::iterator q; while(1){ for(j=0;j<MaxNumVertex;j++) if((AdjMatrix[E.VexNum* MaxNumVertex+j]<MAXNUM) &&( (4) <distance[j])) { distance[j]=E.length+AdjMatrix[E.VexNum* MaxNumVertex+j]; p[j]=E. VexNum; //记录顶点j的前一顶点 MinNode<VertexType, EdgeType> (5) ; H.push_ back(N); push_heap(H. begin(),H.end(),greater<MinNode<VertexType, EdgeType>>()); } if(H.empty()=true)break; //若优先队列为空,那么算法结束 else{ pop_ heap(H.begin(),H. end(),greater<MinNode<VertexType, EdgeType>>()); q=H.end()-1; //从最小化堆中取路径最短的顶点 E=*q; H.pop_ back(); //删除从最小化堆中“挤”出的顶点 } } //end while for(k=0;k<MaxNumVertex;k++){ cout<<"Shorstest path from vertex"<<k<<"is"<<distance[k]<<end1; j=k;cout<<"All vertices are:"; while(j!=source){cout<<j<<"->";j=p[j];} cout<<source<<”.”<<end1; } //打印顶点的最短路径长度和至源点的最短路径上经过的顶点序列 return; }

【参考答案】

[解析] (1)this->length或(*this).length 操作符,的成员函数,比较两个对象的最短路径的长度......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
【说明】设某城市有n个车站,并有m条公交线路连接这些车站,设这些公交车都是单向的,这n个车站被顺序编号为0至n-1。本程序,输入该城市的公交线路数、车站个数,以及各公交线路上的各站编号,求得从站0出发乘公交车至站n-1的最少换车次数。程序利用输入信息构建一张有向图G(用邻接矩阵g表示),有向图的顶点是车站,若有某条公交线路经i站到达j站,就在顶点i到顶点j之间设置一条权为1的有向边<i,j>。如果这样,从站点x至站点y的最少上车次数便对应图G中从点x到点y的最短路径长度。而程序要求的换车次数就是上车次数减1。#include <stdio.h>#define M 20#define N 50int a[N+1]; *用于存放一条线路上的各站编号* int g[N][N]; *严存储对应的邻接矩阵* int dist[N]; *严存储站0到各站的最短路径* int m, n;void buildG(){ int i, j, k, sc, ddprintf(“输入公交线路数,公交站数 n”);scanf( %d%d ,&m,&n);for (i=0;i<n;i++) *邻接矩阵清0* for(j=0;j<n;j++)g[i][j]=0;for(i=0;i<m;i++){ printf( 沿第%d条公交线路的各站编号(0<=编号<=%d,-1结束): n) ,i+1,n-1);sc=0; * 当前线路站计数器* while(1){ scanf( %d ,&dd);if(dd=-1)break;if(dd>=0 && dd<n) (1) ;}a[sc]=-1;for(k=1;a[k]>=0;k++) *处理第i+1条公交线路* for(j=0;j<k;j++)g (2) =1;}}int minLen(){ int j,k;for(j=0;j<n;j++)dist[j]=g[0][j];dist[0]=1;do{for(k=-1,j=0;j<n;j++) *找下一个最少上车次数的站* if(dist[j]>0 &&(k==-1||dist[j]<dist[k]))k=j;if(k<0||k==n-1)break;dist[k]=-dist[k]; *设置k站已求得上车次数的标记* for (j=1;j<n;j++) *调整经过k站能到达的其余各站的上车次数* if( (3) && (dist[j]=0||-dist[k]+1<dist[j]))dist[j]= (4) ;}while(1);j=dist[n-1];return (5) ;}void main(){ int t;buildG();if((t=minLen())<0)printf( 无解! n );elseprintf(“从0号站到%d站需换车%d次 n”,n-1,t);}