Dijkstra 模板
#include <bits/stdc++.h>
using namespace std;
const int maxn = 150005;
const int maxm = 150005;
const int inf = 0x3f3f3f3f;
int n, m;
int head[maxn], ver[maxm], edge[maxm], Next[maxm], d[maxn], tot;
bool vis[maxn];
struct Node{
int pos, val;
bool operator > (const Node &tmp) const{
return val > tmp.val;
}
};
priority_queue<Node, vector<Node>, greater<Node> > pq;
void add(int x, int y, int z){
ver[++ tot] = y, edge[tot] = z;
Next[tot] = head[x], head[x] = tot;
}
void dijkstra(){
memset(d, 0x3f, sizeof(d));
memset(vis, 0, sizeof(vis));
d[1] = 0;
pq.push((Node){1, 0});
while(pq.size()){
Node now = pq.top();
pq.pop();
int x = now.pos;
if(vis[x]) continue;
vis[x] = 1;
for(int i = head[x]; i; i = Next[i]){
int y = ver[i], z = edge[i];
if(vis[y]) continue;
if(d[y] > d[x] + z){
d[y] = d[x] + z;
pq.push((Node){y, d[y]});
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= m; ++ i){
int x, y, z;
cin >> x >> y >> z;
add(x, y, z);
}
dijkstra();
if(d[n] == inf) cout << -1 << endl;
else cout << d[n] << endl;
return 0;
}