模板题
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=150010;
class G{
public:
using callback=function<void (int ,int )>;
G(){
memset(h,-1,sizeof h);
}
void add(int a,int b,int c){
e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
//访问当前节点a的邻居和该边的费用
void adj(int a,callback func){
for(int i=h[a];i!=-1;i=ne[i]) func(e[i],w[i]);
}
int h[N],e[N],ne[N],w[N],idx=0;
};
G g;
int n,m,dist[N],st[N];
typedef pair<int,int> PII;
typedef priority_queue<PII,vector<PII>,greater<PII>> Vipq;//小顶堆
int f(){
memset(dist,0x3f,sizeof dist);
dist[1]=0;
Vipq q;q.push({dist[1],1});
while(q.size()){
auto t=q.top();q.pop();
int a=t.second,d=t.first;
if(st[a]) continue;//跳过重复值
g.adj(a,[&](int b,int w){
if(dist[b]>d+w){
dist[b]=d+w;
if(!st[b]) q.push({dist[b],b});//堆里可能有多个b
}
});
st[a]=1;//已经确定到a的最短距离
}
if(dist[n]==0x3f3f3f3f) return -1;
return dist[n];
}
int main(){
cin>>n>>m;
while(m--){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g.add(a,b,c);
}
cout<<f()<<endl;
return 0;
}