当前位置:网站首页>Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
2022-07-02 12:42:00 【T_ Y_ F666】
Dijkstra AcWing 850. Dijkstra Find the shortest path II
Original link
AcWing 850. Dijkstra Find the shortest path II
Algorithm tags
shortest path Dijkstra
Ideas
The picture is extracted from the solution of the question
The picture is extracted from the solution of the question
Code
#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];
// Sparse graphs are stored in adjacency tables
int h[N], w[N], e[N], ne[N], idx;
bool st[N];
void add(int a,int b,int c){
// It doesn't matter if there are heavy edges , hypothesis 1->2 Weighted as 2 and 3 The edge of , Then traverse to point 1 When 2 The distance of point No. will be updated twice and put into the heap
// In this way, there will be many redundant points in the heap , But the minimum value will pop up when it pops up 2+x(x For the shortest path determined before ),
// Tagged st by true, So next time it pops up 3+x Meeting continue It doesn't go down .
e[idx]=b;
w[idx]=c;
ne[idx]=h[a];
h[a]=idx++;
}
int dij(){
memset(dist, 0x3f , sizeof dist);
dist[1] = 0;
// here heap Why do you want to save pair Well , First, small root piles are arranged according to distance , So there's a variable called distance ,
// Secondly, when taking it out of the heap, you should know which point this point is , Otherwise, how to update the adjacency point ? So the second variable needs to be saved .
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 Just a subscript ,e What is stored in the is i The point corresponding to this subscript .
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";
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support
边栏推荐
- 上手报告|今天聊聊腾讯目前在用的微服务架构
- Intel 内部指令 --- AVX和AVX2学习笔记
- js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)
- bellman-ford AcWing 853. 有边数限制的最短路
- 怎样写一篇赏心悦目的英文数学论文
- Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
- Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
- Adding database driver to sqoop of cdh6
- How to write a pleasing English mathematical paper
- 考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
猜你喜欢
线性DP AcWing 899. 编辑距离
Distributed machine learning framework and high-dimensional real-time recommendation system
Simple use of drools decision table
Deep Copy Event bus
计数类DP AcWing 900. 整数划分
ArrayList与LinkedList效率的对比
Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
包管理工具
中国交通标志检测数据集
Record the range of data that MySQL update will lock
随机推荐
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
Use sqoop to export ads layer data to MySQL
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
Enhance network security of kubernetes with cilium
Leetcode - Sword finger offer 51 Reverse pairs in an array
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
深拷贝 事件总线
趣味 面试题
区间DP AcWing 282. 石子合并
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
上手报告|今天聊聊腾讯目前在用的微服务架构
Sparkcontext: error initializing sparkcontext solution
线性DP AcWing 895. 最长上升子序列
Introduction to CPU instruction set
Visual studio efficient and practical extension tools and plug-ins
[FFH] little bear driver calling process (take calling LED light driver as an example)
Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
计数类DP AcWing 900. 整数划分
防抖 节流
What data types does redis have and their application scenarios