Dijkstra算法实现类—提高,邻接表+优先队列
来源:互联网 发布:最强数据恢复软件 编辑:程序博客网 时间:2024/06/06 09:26
总觉得前点时间写的Dijkstra,有点繁琐,还是用回邻接表和优先队列感觉舒服点。
废话就不多说了,原先那篇邻接矩阵的Dijkstra链接如下:http://blog.csdn.net/betabin/article/details/7375803
这次改为优先队列,则需要一个Vertex结构,记录其序号及到起点的cost。每次push进去队列之前,都刷新其最新cost。并且加了些辅助变量,如判断是否已经处理过该节点的bool数组等。改用邻接表的话,则是感觉空间比较好,感觉也是比较舒服。
这次的代码如下:
#include <iostream>#include <queue>#define MAX_EDGE 30#define MAX_VERTEX 10#define INFINITE 0x7fffffff#define AL_END -1using namespace std;struct Vertex {int vertex;int cost;};bool operator < (const Vertex &a, const Vertex &b){return a.cost > b.cost;}struct AlEdge {int next;int vertex;int cost;};class AlGraph{public:int source;int destination;int edgeNum;int vertexNum;int allCost;void Dijkstra();private:AlEdge edge[MAX_EDGE];priority_queue<Vertex> nextVertexQueue;int alVertex[MAX_VERTEX];bool vertexDealed[MAX_VERTEX];void initialize();void geteData();};void AlGraph::geteData(){cout << "Please input the number of vertex: ";cin >> vertexNum;cout << "Please input the number of edge: ";cin >> edgeNum;cout << "Please input the source: ";cin >> source;cout << "Please input the destination: ";cin >> destination;cout << "Please input the edges.(source, destination, cost.)" << endl;int s, d, c;int ip = 0;for (int i = 0; i < edgeNum; i++){cin >> s >> d >> c;edge[ip].next = alVertex[s];edge[ip].vertex = d;edge[ip].cost = c;alVertex[s] = ip++;}}void AlGraph::initialize(){memset(alVertex, AL_END, sizeof(alVertex));memset(vertexDealed, 0, sizeof(vertexDealed));allCost = 0;while (!nextVertexQueue.empty()){nextVertexQueue.pop();}geteData();}void AlGraph::Dijkstra(){initialize();Vertex currentVertex;currentVertex.cost = 0;currentVertex.vertex = source;nextVertexQueue.push(currentVertex);while (!nextVertexQueue.empty()){currentVertex = nextVertexQueue.top();nextVertexQueue.pop();if (currentVertex.vertex == destination){break;}if (vertexDealed[currentVertex.vertex]){continue;}vertexDealed[currentVertex.vertex] = true;Vertex nextVertex;for (int i = alVertex[currentVertex.vertex]; i != AL_END; i = edge[i].next){if (!vertexDealed[edge[i].vertex]){nextVertex.vertex = edge[i].vertex;nextVertex.cost = currentVertex.cost + edge[i].cost;nextVertexQueue.push(nextVertex);}}}if (currentVertex.vertex == destination){allCost = currentVertex.cost;}else{allCost = INFINITE;}}int main(){AlGraph test;test.Dijkstra();cout << "The smallest cost is: " << test.allCost << endl;return 0;}
- Dijkstra算法实现类—提高,邻接表+优先队列
- Dijkstra(邻接表+优先队列)
- 使用优先队列+邻接表的Dijkstra算法
- Dijkstra + 优先队列 + 邻接表优化
- 基于邻接表和优先级队列的Dijkstra算法实现
- 图算法 单源最短路径 Dijkstra算法(邻接表/邻接矩阵+优先队列STL)
- (转)图算法单源最短路径Dijkstra算法(邻接表/邻接矩阵+优先队列STL)
- Dijkstra单源最短路径算法; 优先队列+静态数组邻接表; STL优先队列还是没想明白排序原则;
- 优先队列Dijkstra实现最短路算法
- 利用优先队列实现的dijkstra算法
- Sicily 1031 Campus(Dijkstra[邻接表+优先队列优化])
- Sicily 1031 Campus(Dijkstra[邻接表+优先队列优化])
- HDU 2544 最短路 静态邻接表+优先队列 dijkstra
- Dijkstra[两种邻接表+优先队列优化]
- 【POJ1511】Invitation Cards 【dijkstra+优先队列+邻接表】
- hdu 2544 最短路(Dijkstra 邻接表+优先队列)
- 【HDU 1874 2544 2066 2112】 Dijkstra单源最短路径专题 —— 优先队列+邻接表/邻接矩阵
- 图论--Dijkstra算法----邻接表实现
- delphi 接口
- 电脑双网卡同时联网
- Linux命令行技巧
- ORACLE中日期和时间函数汇总(转载)
- 自己制作一个等待加载的一个效果
- Dijkstra算法实现类—提高,邻接表+优先队列
- android 关于判断scrollView滚动停止
- Linux系统架构与目录解析读书笔记_第1至第4章
- MFC线程同步
- UDP编程中遇到的一些问题总结
- static inline Linux内联函数
- 分解质因数
- destoon 目录文件结构
- 关于IPv4协议的一点看法-地址空间,分段标识,LinuxNAT