dijkstar-stl堆优化版本
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N = 1e5 + 10;
int n, m;
int dist[N];
bool st[N];
int h[N], e[N], ne[N], w[N], idx;
typedef pair<int, int> PII; //存<与源点距离,编号>
void add(int a, int b, int c) {
e[idx] = b;
ne[idx] = h[a];
w[idx] = c;
h[a] = idx ++ ;
}
int main() {
memset(h, -1, sizeof h);
memset(dist, 0x3f, sizeof dist);
priority_queue<PII, vector<PII>, greater<PII>> heap;
cin >> n >> m;
for (int i = 0; i < m; i ++ ) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
dist[1] = 0;
heap.push({0, 1});
while (heap.size()) {
auto t = heap.top();
heap.pop();
int v = t.second, d = t.first; //v是当前点的编号,d是当前点距离起点的距离
if (st[v]) continue;
st[v] = true;
for (int i = h[v]; i != -1; i = ne[i]) {
int j = e[i];
if (dist[j] > dist[v] + w[i]) {
dist[j] = dist[v] + w[i];
heap.push({dist[j], j});
}
}
}
if (dist[n] != 0x3f3f3f3f) cout << dist[n] << endl;
else cout << -1 << endl;
return 0;
}