dijkstra算法代码实现。输入:第一行,三个整数,$vn,en,s $,用空格分开,分别表示图G中:顶点数目(顶点编号从0开始),弧的数量(无向图每对顶点之间有两条弧),Dijkstra算法指定的起点。其中:1≤ vn ≤ 1000, 1≤ en ≤ vn×(vn-1)。注意:案例中提供的弧可以有重复的,图应该按照最后一次的弧的设置为准,即SetEdge的语义当存在该弧时,设置权值。 之后会有en行,每行表示一对顶点之间的一条弧,形式为:"<"+from+", "+to+", "+weight+">",其中from为弧的起点,to为弧的终点,weight为弧上权值(每对顶点之间的不同的弧上的权值可以不同)输出:共vn行,第i行表示编号为i的顶点(第i个顶点,i从0开始)到起点s的最短路径长度len,以及该最短路径中的前驱顶点p(起始顶点s的前驱设为自身顶点编号s)。示例:输入10 32 0 <2, 1, 2> <1, 2, 2> <5, 8, 52> <8, 5, 52> <1, 7, 141> <7, 1, 141> <0, 8, 185> <8, 0, 185> <3, 8, 43> <8, 3, 43> <7, 8, 136> <8, 7, 136> <1, 2, 122> <2, 1, 122> <6, 8, 150> <8, 6, 150> <2, 1, 6> <1, 2, 6> <6, 3, 16> <3, 6, 16> <7, 9, 34> <9, 7, 34> <3, 1, 10> <1, 3, 10> <8, 9, 193> <9, 8, 193> <7, 8, 49> <8, 7, 49> <0, 1, 198> <1, 0, 198> <0, 5, 179> <5, 0, 179>输出:<0, 0, 0> <1, 198, 0> <2, 204, 1> <3, 208, 1> <4, INF, 0> <5, 179, 0> <6, 224, 3> <7, 234, 8> <8, 185, 0> <9, 268, 7>

时间: 2023-06-12 19:05:47 浏览: 56
```cpp #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; typedef long long ll; const int INF = 0x3f3f3f3f; const int maxn = 1e3 + 10; const int maxm = 1e6 + 10; int vn, en, s; int head[maxn], cnt; int dis[maxn], vis[maxn], p[maxn]; struct Edge { int to, nxt, w; }e[maxm]; inline void AddEdge(int u, int v, int w) { e[++cnt].to = v; e[cnt].w = w; e[cnt].nxt = head[u]; head[u] = cnt; } inline void Init() { memset(head, -1, sizeof(head)); memset(dis, INF, sizeof(dis)); memset(vis, 0, sizeof(vis)); memset(p, -1, sizeof(p)); cnt = -1; } inline void Dijkstra() { priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q; q.push(make_pair(0, s)); dis[s] = 0; while(!q.empty()) { int u = q.top().second; q.pop(); if(vis[u]) continue; vis[u] = 1; for(int i = head[u]; ~i; i = e[i].nxt) { int v = e[i].to, w = e[i].w; if(dis[v] > dis[u] + w) { dis[v] = dis[u] + w; p[v] = u; q.push(make_pair(dis[v], v)); } } } } int main() { Init(); scanf("%d%d%d", &vn, &en, &s); for(int i = 1; i <= en; i++) { int u, v, w; char c; scanf(" <%d, %d, %d%c", &u, &v, &w, &c); AddEdge(u, v, w); if(c != '>') { scanf(" %c", &c); scanf("%d", &w); AddEdge(v, u, w); } } Dijkstra(); for(int i = 0; i < vn; i++) { if(dis[i] == INF) printf("<%d, INF, %d>\n", i, p[i]); else printf("<%d, %d, %d>\n", i, dis[i], p[i]); } return 0; } ```

相关推荐

最新推荐

recommend-type

Dijkstra算法最短路径的C++实现与输出路径

今天小编就为大家分享一篇关于Dijkstra算法最短路径的C++实现与输出路径,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
recommend-type

Dijkstra’s算法:的实现

算法的实现,还有具体的实验报告 采用Microsoft Visual C++ 6.0实现,并且图的存储结构为邻接表。
recommend-type

####这是一篇对python的详细解析

python
recommend-type

菜日常菜日常菜日常菜日常

菜日常菜日常菜日常菜日常
recommend-type

VB学生档案管理系统设计(源代码+论文).rar

计算机专业毕业设计VB精品论文资源
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

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

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