0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00

850. Dijkstra求最短路 II

https://www.acwing.com/problem/content/submission/code_detail/6522838/
This commit is contained in:
Baoshuo Ren 2021-07-18 22:01:05 +08:00 committed by Baoshuo Ren
parent 9764800d02
commit 8f125a7b89
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

40
AcWing/850/850.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <bits/stdc++.h>
using namespace std;
int n, m, u, v, w, dist[200005];
vector<pair<int, int>> g[200005];
bool st[200005];
void dijkstra() {
memset(dist, 0x3f, sizeof(dist));
dist[1] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
q.push(make_pair(0, 1));
while (!q.empty()) {
auto t = q.top();
q.pop();
int tw = t.second;
int td = t.first;
if (st[tw]) continue;
for (auto i : g[tw]) {
int j = i.first;
if (dist[j] > td + i.second) {
dist[j] = td + i.second;
q.push(make_pair(dist[j], j));
}
}
st[tw] = true;
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> u >> v >> w;
g[u].push_back(make_pair(v, w));
}
dijkstra();
cout << (dist[n] == 0x3f3f3f3f ? -1 : dist[n]) << endl;
return 0;
}