From 2fdd39286a35f2a5d0e396953fafe311f080e6fd Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Wed, 21 Jul 2021 14:09:09 +0800 Subject: [PATCH] =?UTF-8?q?P1629=20=E9=82=AE=E9=80=92=E5=91=98=E9=80=81?= =?UTF-8?q?=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R53781286 --- Luogu/problem/P1629/P1629.cpp | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Luogu/problem/P1629/P1629.cpp diff --git a/Luogu/problem/P1629/P1629.cpp b/Luogu/problem/P1629/P1629.cpp new file mode 100644 index 00000000..8470cee0 --- /dev/null +++ b/Luogu/problem/P1629/P1629.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +int n, m, u, v, w, dist1[1005], dist2[1005], ans; +vector> g1[1005], g2[1005]; +bool st1[1005], st2[1005]; + +void dijkstra1() { + memset(dist1, 0x3f, sizeof(dist1)); + dist1[1] = 0; + priority_queue, vector>, greater>> q; + q.push(make_pair(0, 1)); + while (!q.empty()) { + auto t = q.top(); + q.pop(); + if (st1[t.second]) continue; + for (auto i : g1[t.second]) { + if (dist1[i.first] > t.first + i.second) { + dist1[i.first] = t.first + i.second; + q.push(make_pair(dist1[i.first], i.first)); + } + } + st1[t.second] = true; + } +} + +void dijkstra2() { + memset(dist2, 0x3f, sizeof(dist2)); + dist2[1] = 0; + priority_queue, vector>, greater>> q; + q.push(make_pair(0, 1)); + while (!q.empty()) { + auto t = q.top(); + q.pop(); + if (st2[t.second]) continue; + for (auto i : g2[t.second]) { + if (dist2[i.first] > t.first + i.second) { + dist2[i.first] = t.first + i.second; + q.push(make_pair(dist2[i.first], i.first)); + } + } + st2[t.second] = true; + } +} + +int main() { + cin >> n >> m; + for (int i = 1; i <= m; i++) { + cin >> u >> v >> w; + g1[u].push_back(make_pair(v, w)); + g2[v].push_back(make_pair(u, w)); + } + dijkstra1(); + dijkstra2(); + for (int i = 2; i <= n; i++) { + ans += dist1[i] + dist2[i]; + } + cout << ans << endl; + return 0; +}