当前位置:网站首页>(dijkstra+ shortest path + edge traversal 0 (m)) acwing 850 Dijkstra finding the shortest path II
(dijkstra+ shortest path + edge traversal 0 (m)) acwing 850 Dijkstra finding the shortest path II
2022-06-13 09:24:00 【Age worry】
850. Dijkstra Find the shortest path II
Topic link https://www.acwing.com/problem/content/852/
subject :
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<utility>
#include<queue>
using namespace std;
const int N=2e5+10;
int n,m;
bool vis[N];
vector<pair<int ,int > > a[N];
int dijkstra(){
priority_queue<pair<int ,int > ,vector<pair<int ,int >> ,greater<pair<int ,int >>> q;
q.push({
0,1});
bool flag=0;
int sum=0;
while(q.size()){
auto tmp=q.top();
q.pop();
if(vis[tmp.second]) continue;
vis[tmp.second]=1;
if(tmp.second==n){
flag=1;
sum=tmp.first;
}
for(int i=0;i<a[tmp.second].size();i++){
if(!vis[a[tmp.second][i].first]){
q.push({
tmp.first+a[tmp.second][i].second,a[tmp.second][i].first});
}
}
}
if(flag) return sum;
return -1;
}
int main(){
cin>>n>>m;
int x,y,z;
for(int i=0;i<m;i++){
scanf("%d%d%d",&x,&y,&z);
a[x].push_back({
y,z});
}
cout<<dijkstra();
return 0;
}
边栏推荐
- Neo4j環境搭建
- 20211104 why are the traces of similar matrices the same
- C language 7-13 day K candle chart (15 points)
- (dp+ memory) acwing 901 skiing
- LeetCode 72. 编辑距离
- 批量讀取文件夾下的全部語音文件
- LeetCode 1143. 最长公共子序列
- C language: Address Book
- LeetCode 6097. Match after replacing characters (Dictionary)
- Simple implementation of database link pool
猜你喜欢
C language: file operation
an error occurred while trying to rename a file in the destination directory code 5
JUC 原子累加器 源码之 LongAdder
Class loading overview
20220524 how to install coppeliasim to disk D
JUC atomic reference and ABA problem
Jenkins接入Openldap用戶認證
Yolov5 face learning notes
Online debugging tool Arthas advanced
IP address introduction
随机推荐
1-2 24:00 (20 points) [CSP certification true question]
Lecture par lots de tous les fichiers vocaux sous le dossier
Remember! Don't be too confident in writing code! Be sure to write some key log info output, or the problem will not be located.
LeetCode 202. Happy number
turtle库显示系统时间
LeetCode 583. 两个字符串的删除操作
C language: preprocessing in program environment
20211104 why is the trace of a matrix equal to the sum of eigenvalues, and why is the determinant of a matrix equal to the product of eigenvalues
C language: sanziqi
Neo4j環境搭建
Class loading overview
Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing
阿里高级专家剖析 | Protocol的标准设计
Final principle
Solov2 source code analysis
Immutability of shared model
20211020 academician all drive system
批量读取文件夹下的全部语音文件
20211108 differential tracker
Jenkins接入Openldap用户认证