From 8f125a7b89047ff38d747bd0429d871227d9fde8 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Sun, 18 Jul 2021 22:01:05 +0800 Subject: [PATCH] =?UTF-8?q?850.=20Dijkstra=E6=B1=82=E6=9C=80=E7=9F=AD?= =?UTF-8?q?=E8=B7=AF=20II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/6522838/ --- AcWing/850/850.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 AcWing/850/850.cpp diff --git a/AcWing/850/850.cpp b/AcWing/850/850.cpp new file mode 100644 index 00000000..d04aa050 --- /dev/null +++ b/AcWing/850/850.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int n, m, u, v, w, dist[200005]; +vector> g[200005]; +bool st[200005]; + +void dijkstra() { + memset(dist, 0x3f, sizeof(dist)); + dist[1] = 0; + priority_queue, vector>, greater>> 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; +}