AcWing 850. Dijkstra求最短路 II 堆优化版 优先队列 稀疏图
//稀疏图 点和边差不多
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 1e5 + ;
int n, m;
int h[N], e[N], ne[N], idx;
int w[N];//表示权值
int dist[N];
bool st[N];
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
int dijkstra() {
memset(dist, 0x3f, sizeof dist);
dist[] = ;
priority_queue<PII, vector<PII>, greater<PII>> heap;//小根堆
heap.push({, });//起点到起点的距离为0
while (heap.size()) {
auto t = heap.top();//找到距离最小的点
heap.pop();//删掉
int ver = t.second, distance = t.first;
if (st[ver]) continue;//如果这个点之前已经出来过,就跳过
st[ver] = true;//
for (int i = h[ver]; i != -; i = ne[i]) {//遍历所有临边
int j = e[i];
if (dist[j] > distance + w[i]) {
dist[j] = distance + w[i];
heap.push({dist[j], j});
}
}
}
if (dist[n] == 0x3f3f3f3f) return -;
return dist[n];
}
int main() {
scanf("%d%d", &n, &m);
memset(h, -, sizeof h);
while (m -- ) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
cout << dijkstra() << endl;
return ;
}
AcWing 850. Dijkstra求最短路 II 堆优化版 优先队列 稀疏图的更多相关文章
- acwing 850. Dijkstra求最短路 II 模板
地址 https://www.acwing.com/problem/content/description/852/ 给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为非负值. 请你求 ...
- 850. Dijkstra求最短路 II
给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为正值. 请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出-1. 输入格式 第一行包含整数n和m. 接下来m行每行包 ...
- 850. Dijkstra求最短路 II(堆优化模板)
给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为非负值. 请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出-1. 输入格式 第一行包含整数n和m. 接下来m行每行 ...
- ACM - 最短路 - AcWing 849 Dijkstra求最短路 I
AcWing 849 Dijkstra求最短路 I 题解 以此题为例介绍一下图论中的最短路算法.先让我们考虑以下问题: 给定一个 \(n\) 个点 \(m\) 条边的有向图(无向图),图中可能存在重边 ...
- acwing 849 Dijkstra求最短路 I 模板
地址 https://www.acwing.com/problem/content/description/851/ 给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为正值. 请你求出 ...
- AcWing 849. Dijkstra求最短路 I 朴素 邻接矩阵 稠密图
//朴素Dijkstra 边权都是正数 稠密图:点和边差的比较多 #include<cstring> #include<iostream> #include<algori ...
- 朴素版和堆优化版dijkstra和朴素版prim算法比较
1.dijkstra 时间复杂度:O(n^2) n次迭代,每次找到距离集合S最短的点 每次迭代要用找到的点t来更新其他点到S的最短距离. #include<iostream> #inclu ...
- 洛谷P3371单源最短路径Dijkstra堆优化版及优先队列杂谈
其实堆优化版极其的简单,只要知道之前的Dijkstra怎么做,那么堆优化版就完全没有问题了. 在做之前,我们要先学会优先队列,来完成堆的任务,下面盘点了几种堆的表示方式. priority_queue ...
- ACM - 最短路 - AcWing 851 spfa求最短路
AcWing 851 spfa求最短路 题解 以此题为例介绍一下图论中的最短路算法 \(Bellman\)-\(Ford\) 算法.算法的步骤和正确性证明参考文章最短路径(Bellman-Ford算法 ...
随机推荐
- Ubuntu使用ipython出现版本不兼容的问题
今天租了一个ECS,然后准备安装了jupyter,准备配置,输入ipython,出现如下报错 ImportError: IPython 7.10+ supports Python 3.6 and ab ...
- Wannafly Camp 2020 Day 7H 游戏 - 欧拉筛,GCD
忘记特判 \(1\) ,血了一地 听说 \(O(n^2 \log n)\) 能过? #include <bits/stdc++.h> #define int long long using ...
- macOS 下安装tomcat
The Servlet 4.0 specification is out and Tomcat 9.0.x does support it. Time to dive into Tomcat 9. [ ...
- PP: Triple-shapelet networks for time series classification
Problem: time series classification shapelet-based method: two issues 1. for multi-class imbalanced ...
- C++构造函数和重载函数运算符如何区分
构造函数和重载函数运算符如何区分: class Distance { private: int feet; int inches; public: Distance(){ feet = ; inche ...
- AntDesign(React)学习-2 第一个页面
1.前面创建了第一个项目jgdemo,结构如下,使用TypeScript. 2.yarn start启动项目 3.点击GettingStarted是umi的官方网站 https://umijs.org ...
- 利用Marshal来管理非托管资源
void MarshalChartDemo() { string name = "xuwei"; IntPtr pName = Marshal.AllocHGlobal(name. ...
- MVC5+EF6入门完整教程6:Partial View
https://i-beta.cnblogs.com/posts/edit 上篇文章提到过Partial和Action这两个helper, 本篇文章主要就结合这两个helper来讲解分部视图(Part ...
- Django | 解决“(1146, "Table 'mydb.django_session' doesn't exist")”报错的方法
我只写了下面一行 就生成了session表 manage.py makemigrations sessions manage.py migrate sessions 参考:https://www.cn ...
- wcf编程资料
如下为WCF编辑资料 链接:https://pan.baidu.com/s/1kZnc6eNOfEggHSfJNXj8Ag 提取码:gj7s 复制这段内容后打开百度网盘手机App,操作更方便哦 第01 ...