Dijkstra算法实现类—提高,邻接表+优先队列

总觉得前点时间写的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 -1

using 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算法
    原文地址: https://blog.csdn.net/betabin/article/details/7390403
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞