From 24bbdf0fdfde31a80bd843322713d284723e21d6 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Wed, 11 Aug 2021 20:52:48 +0800 Subject: [PATCH] =?UTF-8?q?851.=20spfa=E6=B1=82=E6=9C=80=E7=9F=AD=E8=B7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/7061781/ --- AcWing/851/851.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 AcWing/851/851.cpp diff --git a/AcWing/851/851.cpp b/AcWing/851/851.cpp new file mode 100644 index 00000000..c13ffb76 --- /dev/null +++ b/AcWing/851/851.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +int n, m, u, v, w, dist[100005]; +vector> g[100005]; + +void spfa() { + memset(dist, 0x3f, sizeof(dist)); + dist[1] = 0; + queue q; + q.push(1); + while (!q.empty()) { + int t = q.front(); + q.pop(); + for (auto i : g[t]) { + if (dist[i.first] > dist[t] + i.second) { + dist[i.first] = dist[t] + i.second; + q.push(i.first); + } + } + } +} + +int main() { + cin >> n >> m; + for (int i = 0; i < m; i++) { + cin >> u >> v >> w; + g[u].push_back(make_pair(v, w)); + } + spfa(); + if (dist[n] == 0x3f3f3f3f) { + cout << "impossible" << endl; + } else { + cout << dist[n] << endl; + } + return 0; +}