用数据结构c++写

时间: 2023-07-04 22:27:13 浏览: 51
好的,下面我给您提供C++代码实现: 首先,我们需要定义景点的结构体,包括代号、名称、简介等属性: ```c++ struct Site { string id; // 代号 string name; // 名称 string intro; // 简介 }; ``` 然后,我们需要定义边的结构体,包括路径长度等属性: ```c++ struct Edge { int length; // 路径长度 }; ``` 接着,我们需要定义图的类,包括邻接表、顶点数和边数等属性: ```c++ class Graph { public: Graph(int n); // 构造函数,n为顶点数 void addSite(int idx, string id, string name, string intro); // 添加顶点 void addEdge(int u, int v, int length); // 添加边 Site getSite(int idx); // 查询顶点信息 int getShortestPath(int u, int v, vector<Site>& path); // 查询最短路径 private: vector<vector<pair<int, Edge>>> adjList; // 邻接表 int n; // 顶点数 int m; // 边数 }; ``` 其中,adjList是邻接表,n是顶点数,m是边数。addSite用于添加顶点,addEdge用于添加边,getSite用于查询顶点信息,getShortestPath用于查询最短路径,并返回路径上的景点信息。 下面是Graph类的实现: ```c++ Graph::Graph(int n) { this->n = n; this->m = 0; adjList.resize(n); } void Graph::addSite(int idx, string id, string name, string intro) { Site site = {id, name, intro}; adjList[idx].push_back(make_pair(idx, Edge())); } void Graph::addEdge(int u, int v, int length) { Edge edge = {length}; adjList[u].push_back(make_pair(v, edge)); adjList[v].push_back(make_pair(u, edge)); // 如果是无向图,则需要添加反向边 m++; } Site Graph::getSite(int idx) { return adjList[idx][0].first; } int Graph::getShortestPath(int u, int v, vector<Site>& path) { vector<int> dist(n, INT_MAX); // 到各个顶点的距离 vector<bool> visited(n, false); // 是否已经访问 vector<int> prev(n, -1); // 记录路径 priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; // 小根堆 dist[u] = 0; pq.push(make_pair(0, u)); while (!pq.empty()) { auto top = pq.top(); pq.pop(); int curr = top.second; if (visited[curr]) continue; visited[curr] = true; for (auto& p : adjList[curr]) { int next = p.first; int length = p.second.length; if (dist[next] > dist[curr] + length) { dist[next] = dist[curr] + length; prev[next] = curr; pq.push(make_pair(dist[next], next)); } } } if (prev[v] == -1) return -1; // 无法到达 int curr = v; while (curr != -1) { path.push_back(getSite(curr)); curr = prev[curr]; } reverse(path.begin(), path.end()); return dist[v]; } ``` 其中,getShortestPath使用了 Dijkstra 算法,使用小根堆进行优化。最终,我们就可以通过Graph类来实现校园平面图的存储、查询和最短路径计算等功能了!

相关推荐

最新推荐

recommend-type

C++数据结构与算法之双缓存队列实现方法详解

主要介绍了C++数据结构与算法之双缓存队列实现方法,结合实例形式分析了双缓存队列的原理、实现方法与相关注意事项,需要的朋友可以参考下
recommend-type

基于QT C++实现的数据结构软件设计报告

哈工大(威海)计算机科学与技术学院 软件设计程序II的实验报告,基于QT,C++实现的简单饮食健康助手小程序,具有一定的数据结构知识的构建。原作者,可私聊源码。
recommend-type

数据结构c++实现程序

数据结构c++实现 介绍了点关于数据结构的c++实现程序,本人也是在网络上下载的,希望能对其他人有些用处。
recommend-type

数据结构(c++英文版)

1)数据结构(c++英文版),Data structures using c++;里面对STL讲解的还是不错的,比普通的STL源码级的还好; 2)重点还是侧重在数据结构的描述,核心函数的代码实现也很不错。 3)很好的一本工具书。
recommend-type

数据结构和算法分析 C++版 第三版

数据结构c++版 pfd文档 全英文版 practical introduction todata structures and algorithm analysis 第三版
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的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。