图的操作与实现

/ 2,407评论 / 11692阅读 / 0点赞

目录

要求


源代码

若以下显示需要登录,请刷新页面或点击此处下载。


思考

若只求带权有向图G中从顶点i到顶点j的最短路径,如何修改Dijkstra 算法来实现这一功能?

void Dijkstra_way(int first_point)
{
	int* widget_list = new int[map_size];	//记录距离0的长度
	for (int i = map_size; i--;)
	{
		widget_list[i] = INF;
		adjList[i].weight = -1;
	}
	widget_list[first_point] = 0;
	adjList[first_point].weight = 0;
	for (int i = 1; i < map_size; ++i)
		for (int j = map_size; j--;)		//计算距离
			if (adjList[j].weight >= 0)
				for (node* node_p = adjList[j].next_p; node_p != NULL; node_p = node_p->next_p)
					if (widget_list[j] + node_p->weight < widget_list[node_p->data])
					{
						widget_list[node_p->data] = widget_list[j] + node_p->weight;
						adjList[node_p->data].weight = j;
					}
	cout << "<< Dijkstra 最短路径:" << endl;
	coolQueue<int> stack;
	for (int i = 0; i < map_size; ++i)
	{
		for (int j = i; j != first_point;)
		{
			stack.End_push(j);
			j = adjList[j].weight;
		}
		cout << "<< " << first_point;
		for (int pop_int = 0; stack.End_pop(pop_int);)
			cout << " -> " << pop_int;
		cout << endl;
		stack.clear();
	}
	adjList_reflush();
}
  1. this page说道:

    I love reading through a post that will make people think. Also, thank you for allowing for me to comment.

  2. CEO quang huy说道:

    Great information. Lucky me I came across your site by accident (stumbleupon). I have book marked it for later!

  3. This web site truly has all the information and facts I needed concerning this subject and didn’t know who to ask.

  4. You have made some good points there. I checked on the internet for more information about the issue and found most people will go along with your views on this web site.

  5. Everything is very open with a precise description of the challenges. It was truly informative. Your website is useful. Many thanks for sharing!

  6. 78win说道:

    This is the right webpage for anybody who hopes to understand this topic. You realize so much its almost hard to argue with you (not that I actually will need to…HaHa). You definitely put a fresh spin on a topic that’s been written about for many years. Great stuff, just great.

  7. Alphonso Bonning说道:

    Pretty! This has been an extremely wonderful post. Thank you for supplying this information.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注