当前位置:网站首页>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
边栏推荐
- 单指令多数据SIMD的SSE/AVX指令集和API
- 模块化 CommonJS ES Module
- Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
- Rust search server, rust quick service finding tutorial
- kubenetes中port、targetPort、nodePort、containerPort的区别与联系
- 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
- 深拷贝 事件总线
- CPU指令集介绍
- 线性DP AcWing 899. 编辑距离
- 通过反射执行任意类的任意方法
猜你喜欢
浏览器存储方案
"As a junior college student, I found out how difficult it is to counter attack after graduation."
线性DP AcWing 896. 最长上升子序列 II
async/await 异步函数
spfa AcWing 852. spfa判断负环
BOM DOM
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
[FFH] little bear driver calling process (take calling LED light driver as an example)
Sparkcontext: error initializing sparkcontext solution
2.7 binary tree, post order traversal - [FBI tree]
随机推荐
堆 AcWing 839. 模拟堆
js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
Shuttle encapsulated AppBar
Oracle从入门到精通(第4版)
染色法判定二分图 AcWing 860. 染色法判定二分图
线性DP AcWing 899. 编辑距离
模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
LeetCode—剑指 Offer 59 - I、59 - II
Distributed machine learning framework and high-dimensional real-time recommendation system
Sweetheart leader: Wang Xinling
JDBC 预防sql注入问题与解决方法[PreparedStatement]
绕过ObRegisterCallbacks需要驱动签名方法
What data types does redis have and their application scenarios
LeetCode—<动态规划专项>剑指 Offer 19、49、60
正确遍历EntryList方法
Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)
When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
Wechat official account payment prompt MCH_ ID parameter format error
BOM DOM
bellman-ford AcWing 853. 有边数限制的最短路