当前位置:网站首页>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 
边栏推荐
- Openssh remote enumeration username vulnerability (cve-2018-15473)
- This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
- Mongodb redis differences
- async/await 异步函数
- [ybtoj advanced training guidance] cross the river [BFS]
- Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
- Fluent fluent library encapsulation
- Direct control PTZ PTZ PTZ PTZ camera debugging (c)
- Leetcode - Sword finger offer 51 Reverse pairs in an array
- Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold
猜你喜欢
随机推荐
考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
高性能纠删码编码
What data types does redis have and their application scenarios
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
Fluent fluent library encapsulation
When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
AI中台技术调研
The second composition template of postgraduate entrance examination English / chart composition, English chart composition is enough
bellman-ford AcWing 853. 有边数限制的最短路
arcgis js 4. Add pictures to x map
Leetcode - Sword finger offer 59 - I, 59 - II
Distributed machine learning framework and high-dimensional real-time recommendation system
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)
线性DP AcWing 896. 最长上升子序列 II
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
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
JDBC 预防sql注入问题与解决方法[PreparedStatement]
JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)








