1 条题解
-
1
参考答案:
#include<iostream> #include<queue> #include<vector> #include<cstring> using namespace std; const int N = 200010; typedef pair<int, int> PII; int e[N], ne[N], h[N], w[N], idx; int st[N]; int n, m; int dis[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(dis, 0x3f, sizeof(dis)); dis[1] = 0; priority_queue<PII, vector<PII>, greater<PII>> heap; heap.push({ 0,1 }); while (heap.size()) { auto t = heap.top(); heap.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 (dis[j] > dis[ver] + w[i]) { dis[j] = dis[ver] + w[i]; heap.push({ dis[j],j }); } } } if (dis[n] == 0x3f3f3f3f) return -1; else return dis[n]; } int main() { memset(h, -1, sizeof(h)); cin >> n >> m; for (int i = 1; i <= m; ++i) { int a, b, c; cin >> a >> b >> c; add(a, b, c); } cout << dijkstra() << endl; return 0; }
- 1
信息
- ID
- 95
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 10
- 已通过
- 1
- 上传者