用迪杰斯特拉算法求一个点到其他点的最短路径,用堆排序优化,图用邻接多重表储存,用C语言代码写一个程序

时间: 2023-06-23 20:08:29 浏览: 46
好的,下面是C语言代码实现: ```c #include <stdio.h> #include <stdlib.h> #include <limits.h> // 邻接多重表的边结构体 typedef struct Edge { int vertex1; int vertex2; int weight; struct Edge *next1; struct Edge *next2; } Edge; // 邻接多重表的顶点结构体 typedef struct Vertex { int data; Edge *first_edge; } Vertex; // 堆结构体 typedef struct Heap { int *data; int size; int capacity; } Heap; // 初始化堆 Heap *init_heap(int capacity) { Heap *heap = (Heap *)malloc(sizeof(Heap)); heap->data = (int *)malloc(sizeof(int) * (capacity + 1)); heap->size = 0; heap->capacity = capacity; return heap; } // 插入元素到堆中 void insert_heap(Heap *heap, int element) { if (heap->size == heap->capacity) { return; } heap->data[++heap->size] = element; int i = heap->size; while (i > 1 && heap->data[i] < heap->data[i / 2]) { int temp = heap->data[i]; heap->data[i] = heap->data[i / 2]; heap->data[i / 2] = temp; i /= 2; } } // 弹出堆顶元素 int pop_heap(Heap *heap) { if (heap->size == 0) { return -1; } int ret = heap->data[1]; heap->data[1] = heap->data[heap->size--]; int i = 1, j = 2; while (j <= heap->size) { if (j + 1 <= heap->size && heap->data[j + 1] < heap->data[j]) { j = j + 1; } if (heap->data[i] > heap->data[j]) { int temp = heap->data[i]; heap->data[i] = heap->data[j]; heap->data[j] = temp; i = j; j = i * 2; } else { break; } } return ret; } // 判断堆是否为空 int is_empty_heap(Heap *heap) { return heap->size == 0; } // 释放堆内存 void free_heap(Heap *heap) { free(heap->data); free(heap); } // 初始化邻接多重表 void init_graph(Vertex *graph, int n) { for (int i = 0; i < n; i++) { graph[i].data = i + 1; graph[i].first_edge = NULL; } } // 添加边到邻接多重表中 void add_edge(Vertex *graph, int vertex1, int vertex2, int weight) { Edge *edge = (Edge *)malloc(sizeof(Edge)); edge->vertex1 = vertex1; edge->vertex2 = vertex2; edge->weight = weight; edge->next1 = graph[vertex1 - 1].first_edge; edge->next2 = graph[vertex2 - 1].first_edge; graph[vertex1 - 1].first_edge = edge; graph[vertex2 - 1].first_edge = edge; } // 释放邻接多重表内存 void free_graph(Vertex *graph, int n) { for (int i = 0; i < n; i++) { Edge *edge = graph[i].first_edge; while (edge != NULL) { Edge *temp = edge; edge = edge->next1 == edge ? NULL : edge->next1; free(temp); } } } // 迪杰斯特拉算法求解最短路径 void dijkstra(Vertex *graph, int n, int start, int *dist) { // 初始化距离数组 for (int i = 0; i < n; i++) { dist[i] = INT_MAX; } dist[start - 1] = 0; // 初始化堆 Heap *heap = init_heap(n); insert_heap(heap, start); // 迭代更新距离数组 while (!is_empty_heap(heap)) { int vertex = pop_heap(heap); Edge *edge = graph[vertex - 1].first_edge; while (edge != NULL) { int next_vertex = edge->vertex1 == vertex ? edge->vertex2 : edge->vertex1; if (dist[next_vertex - 1] > dist[vertex - 1] + edge->weight) { dist[next_vertex - 1] = dist[vertex - 1] + edge->weight; insert_heap(heap, next_vertex); } edge = edge->next1 == edge ? NULL : (edge->next1->vertex1 == vertex ? edge->next1->next2 : edge->next1); } } // 释放堆内存 free_heap(heap); } int main() { int n = 5; Vertex *graph = (Vertex *)malloc(sizeof(Vertex) * n); init_graph(graph, n); add_edge(graph, 1, 2, 10); add_edge(graph, 1, 4, 30); add_edge(graph, 1, 5, 100); add_edge(graph, 2, 3, 50); add_edge(graph, 3, 5, 10); add_edge(graph, 4, 3, 20); add_edge(graph, 4, 5, 60); int start = 1; int *dist = (int *)malloc(sizeof(int) * n); dijkstra(graph, n, start, dist); printf("start from %d:\n", start); for (int i = 0; i < n; i++) { printf("%d -> %d: %d\n", start, i + 1, dist[i]); } free(dist); free_graph(graph, n); free(graph); return 0; } ``` 这个程序实现了一个邻接多重表的图数据结构,使用了迪杰斯特拉算法求解最短路径,并用堆排序优化了算法。程序输出了从第一个点出发到其他点的最短路径。

相关推荐

最新推荐

recommend-type

C++用Dijkstra(迪杰斯特拉)算法求最短路径

Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。下面这篇文章就给大家介绍关于C++用Dijkstra算法...
recommend-type

麦肯锡-年月―中国xx集团战略咨询项目建议书.ppt

麦肯锡-年月―中国xx集团战略咨询项目建议书.ppt
recommend-type

廖倩5.14运营款.xlsx

廖倩5.14运营款.xlsx
recommend-type

setuptools-25.0.2-py2.py3-none-any.whl

文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依