#include <bits/stdc++.h> using namespace std; const int N = 1000010; int h[N], e[N], ne[N], w[N], idx; int dist[N]; bool st[N]; int m, n; void add(int a, int b, int c) { w[idx] = c, e[idx] = b, ne[idx] = h[a], h[a] = idx++; } int dijkstra() { memset(dist,0x3f,sizeof dist); dist[1] = 0; priority_queue<pair<int,int>> q; q.push(make_pair(0,1)); while(q.size()) { auto t = q.top(); q.pop(); int ver = t.second; if(st[ver]) continue; st[ver] = true; for(int i = h[ver]; i != -1; i = ne[i]) { int j = e[i]; if(dist[j] > dist[ver] + w[i]) { dist[j] = dist[ver] + w[i]; q.push(make_pair(-dist[j],j)); // 加入-dist[j]变为小堆 } } } if(dist[n] == 0x3f3f3f3f) return -1; return dist[n]; } int main() { scanf("%d%d",&n,&m); memset(h,-1,sizeof h); while(m -- ) { int a, b , c; scanf("%d%d%d",&a,&b,&c); add(a,b,c); } int t = dijkstra(); printf("%d ",t); return 0; }