当前位置:网站首页>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";
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- drools中then部分的写法
- Drools executes string rules or executes a rule file
- Input box assembly of the shutter package
- Input a three digit number and output its single digit, ten digit and hundred digit.
- Docker-compose配置Mysql,Redis,MongoDB
- Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
- 初始JDBC 编程
- ThreadLocal的简单理解
- Deep copy event bus
- CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
猜你喜欢
AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
Record the range of data that MySQL update will lock
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
Go learning notes - multithreading
Embedded Software Engineer career planning
1380. Lucky numbers in the matrix [two-dimensional array, matrix]
CDA data analysis -- Introduction and use of aarrr growth model
mysql数据库基础
mysql表的增删改查(进阶)
随机推荐
Use sqoop to export ads layer data to MySQL
The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
Leetcode739 daily temperature
Fastdateformat why thread safe
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
AI中台技术调研
区间DP AcWing 282. 石子合并
分布式机器学习框架与高维实时推荐系统
Lombok common annotations
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
Sort---
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
趣味 面试题
中国交通标志检测数据集
甜心教主:王心凌
深拷貝 事件總線
寻找二叉树中任意两个数的公共祖先
[ybtoj advanced training guide] similar string [string] [simulation]
Intel 内部指令 --- AVX和AVX2学习笔记
IPhone 6 plus is listed in Apple's "retro products" list