模板得熟练背会
#include <iostream>
#include <cstring>
#include <algorithm>
#include<queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 1e6;
int e[N],ne[N],h[N],idx;
int w[N];
int n,m;
int dist[N];
bool st[N];
int dijkstra(){
memset (dist,0x3f,sizeof(dist));
dist[0]=0;
priority_queue<PII,vector<PII>,greater<PII>> q;
q.push({0,1});
while (q.size()){
PII k = q.top();
q.pop();
int a = k.first,b= k.second;
if (st[b]) continue;
st[b] = true;
for (int i = h[b];i!=-1;i=ne[i]){
int c = e[i];
if (st[c]) continue;
if (dist[c]>a+w[i]){
dist[c] = a+w[i];
q.push({dist[c],c});
}
}
}
if (dist[n] ==0x3f3f3f3f) return -1;
else return dist[n];
}
void add (int a,int b,int c){
w[idx] = c;
e[idx] = b;
ne[idx]=h[a];
h[a] = idx++;
}
int main(){
memset (h,-1,sizeof(h));
int a,b,c;
scanf("%d%d", &n, &m);
while (m--){
scanf("%d%d%d", &a, &b, &c);
add(a,b,c);
}
cout<<dijkstra()<<endl;;
return 0;
}