From 764424125cbdb1646f90a0b2242c6feefbe0619b Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Wed, 22 Sep 2021 21:26:22 +0800 Subject: [PATCH] =?UTF-8?q?1127.=20=E9=A6=99=E7=94=9C=E7=9A=84=E9=BB=84?= =?UTF-8?q?=E6=B2=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/7861091/ --- AcWing/1127/1127.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 AcWing/1127/1127.cpp diff --git a/AcWing/1127/1127.cpp b/AcWing/1127/1127.cpp new file mode 100644 index 00000000..bdef3a4d --- /dev/null +++ b/AcWing/1127/1127.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +int n, p, c, a[505], dist[805], u, v, w, ans = 0x3f3f3f3f; +bool vis[805]; +vector> g[805]; + +int dijkstra(int s) { + memset(dist, 0x3f, sizeof(dist)); + memset(vis, 0x00, sizeof(vis)); + dist[s] = 0; + priority_queue, vector>, greater>> q; + q.push(make_pair(0, s)); + while (!q.empty()) { + auto t = q.top(); + q.pop(); + if (vis[t.second]) continue; + for (auto i : g[t.second]) { + if (dist[i.first] > t.first + i.second) { + dist[i.first] = t.first + i.second; + q.push(make_pair(dist[i.first], i.first)); + } + } + vis[t.second] = true; + } + int sum = 0; + for (int i = 1; i <= n; i++) { + if (dist[a[i]] == 0x3f3f3f3f) return 0x3f3f3f3f; + sum += dist[a[i]]; + } + return sum; +} + +int main() { + cin >> n >> p >> c; + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + for (int i = 1; i <= c; i++) { + cin >> u >> v >> w; + g[u].push_back(make_pair(v, w)); + g[v].push_back(make_pair(u, w)); + } + for (int i = 1; i <= p; i++) { + ans = min(ans, dijkstra(i)); + } + cout << ans << endl; + return 0; +}