当前位置:网站首页>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 
边栏推荐
- NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF
- Rust search server, rust quick service finding tutorial
- 8 examples of using date commands
- spfa AcWing 852. spfa判断负环
- Record the range of data that MySQL update will lock
- Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
- async/await 异步函数
- Wechat official account payment prompt MCH_ ID parameter format error
- Simple understanding of ThreadLocal
- Embedded Software Engineer career planning
猜你喜欢

PR 2021 quick start tutorial, learn about the and functions of the timeline panel

Performance tuning project case

堆 AcWing 838. 堆排序

JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)

BOM DOM

Direct control PTZ PTZ PTZ PTZ camera debugging (c)

Redis transaction mechanism implementation process and principle, and use transaction mechanism to prevent inventory oversold

8 examples of using date commands

Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?

The differences and relationships among port, targetport, nodeport and containerport in kubenetes
随机推荐
1380. Lucky numbers in the matrix [two-dimensional array, matrix]
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
Sparkcontext: error initializing sparkcontext solution
哈希表 AcWing 840. 模拟散列表
LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
Go learning notes - multithreading
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
Dijkstra AcWing 850. Dijkstra求最短路 II
Adding database driver to sqoop of cdh6
染色法判定二分图 AcWing 860. 染色法判定二分图
Calculate the maximum path sum of binary tree
. Net wechat message template push
Leetcode - Sword finger offer 59 - I, 59 - II
[ybtoj advanced training guidance] cross the river [BFS]
spfa AcWing 851. spfa求最短路
一些突然迸发出的程序思想(模块化处理)
Fluent fluent library encapsulation
深拷贝 事件总线
JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
线性DP AcWing 896. 最长上升子序列 II