当前位置:网站首页>Dijkstra AcWing 850. Dijkstra求最短路 II
Dijkstra AcWing 850. Dijkstra求最短路 II
2022-07-02 09:43:00 【T_Y_F666】
Dijkstra AcWing 850. Dijkstra求最短路 II
原题链接
算法标签
最短路 Dijkstra
思路
代码
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int, int>PII;
const int N = 150005;
int n,m;
int dist[N];
// 稀疏图用邻接表来存
int h[N], w[N], e[N], ne[N], idx;
bool st[N];
void add(int a,int b,int c){
// 有重边也不要紧,假设1->2有权重为2和3的边,再遍历到点1的时候2号点的距离会更新两次放入堆中
// 这样堆中会有很多冗余的点,但是在弹出的时候还是会弹出最小值2+x(x为之前确定的最短路径),
// 并标记st为true,所以下一次弹出3+x会continue不会向下执行。
e[idx]=b;
w[idx]=c;
ne[idx]=h[a];
h[a]=idx++;
}
int dij(){
memset(dist, 0x3f , sizeof dist);
dist[1] = 0;
// 这里heap中为什么要存pair呢,首先小根堆是根据距离来排的,所以有一个变量要是距离,
// 其次在从堆中拿出来的时候要知道知道这个点是哪个点,不然怎么更新邻接点呢?所以第二个变量要存点。
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({0,1});
while (heap.size()) {
auto t = heap.top();
heap.pop();
int ver = t.y,dis = t.x;
if(st[ver]){
continue;
}
st[ver] = true;
for(int i = h[ver]; ~i; i = ne[i]){
// i只是个下标,e中在存的是i这个下标对应的点。
int j = e[i];
if (dist[j] > dist[ver] + w[i]){
dist[j] = dist[ver] + w[i];
heap.push({dist[j], j});
}
}
}
if (dist[n] == 0x3f3f3f3f){
return -1;
}
return dist[n];
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
memset(h, -1, sizeof h);
while(m--){
int a,b,c;
cin>>a>>b>>c;
add(a,b,c);
}
cout<<dij()<<"\n";
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- [ybtoj advanced training guide] similar string [string] [simulation]
- Multiply LCA (nearest common ancestor)
- Find the common ancestor of any two numbers in a binary tree
- FBX import under ue4/ue5 runtime
- drools执行指定的规则
- AI mid stage technology research
- drools执行String规则或执行某个规则文件
- 趣味 面试题
- Fastdateformat why thread safe
- Sort---
猜你喜欢
![[ybtoj advanced training guidance] cross the river [BFS]](/img/4e/83f14452ea6410768cdd01e725af2e.jpg)
[ybtoj advanced training guidance] cross the river [BFS]

kubenetes中port、targetPort、nodePort、containerPort的区别与联系

Distributed machine learning framework and high-dimensional real-time recommendation system

高性能纠删码编码

染色法判定二分图 AcWing 860. 染色法判定二分图

Bom Dom

Multiply LCA (nearest common ancestor)

mysql表的增删改查(进阶)

CDH6之Sqoop添加数据库驱动

Use sqoop to export ads layer data to MySQL
随机推荐
Leetcode - Sword finger offer 37, 38
单指令多数据SIMD的SSE/AVX指令集和API
Gaode map test case
FBX import under ue4/ue5 runtime
线性DP AcWing 898. 数字三角形
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
Writing method of then part in drools
Day12 control flow if switch while do While guessing numbers game
[ybtoj advanced training guidance] cross the river [BFS]
Distributed machine learning framework and high-dimensional real-time recommendation system
模块化 CommonJS ES Module
mysql索引和事务
[C language] Yang Hui triangle, customize the number of lines of the triangle
BOM DOM
In development, why do you find someone who is paid more than you but doesn't write any code?
考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
How to write a pleasing English mathematical paper
Introduction to CPU instruction set
The second composition template of postgraduate entrance examination English / chart composition, English chart composition is enough
Leetcode - Sword finger offer 51 Reverse pairs in an array

