From 47dc0cc260ba6ab68a2e01cc5ce78ee456c7d40b Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 5 Dec 2022 19:34:56 +0800 Subject: [PATCH] =?UTF-8?q?P6192=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E6=96=AF=E5=9D=A6=E7=BA=B3=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/96635255 --- Luogu/P6192/P6192.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Luogu/P6192/P6192.cpp diff --git a/Luogu/P6192/P6192.cpp b/Luogu/P6192/P6192.cpp new file mode 100644 index 00000000..aad82db2 --- /dev/null +++ b/Luogu/P6192/P6192.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 105, + K = 12; +const int INF = 0x3f3f3f3f; + +int n, m, k, a[K], f[1 << K][N], ans = INF; +std::vector> g[N]; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m >> k; + + for (int i = 1, u, v, w; i <= m; i++) { + cin >> u >> v >> w; + + g[u].emplace_back(v, w); + g[v].emplace_back(u, w); + } + + memset(f, 0x3f, sizeof(f)); + + for (int i = 1; i <= k; i++) { + cin >> a[i]; + + f[1 << (i - 1)][a[i]] = 0; + } + + for (int s = 0; s < 1 << k; s++) { + std::queue q; + std::vector vis(n + 1); + + for (int i = 1; i <= n; i++) { + for (int t = s & (s - 1); t; t = (t - 1) & s) { + f[s][i] = std::min(f[s][i], f[t][i] + f[s ^ t][i]); + } + + if (f[s][i] != INF) { + q.emplace(i); + vis[i] = true; + } + } + + // SPFA + while (!q.empty()) { + int u = q.front(); + q.pop(); + + vis[u] = false; + + for (auto e : g[u]) { + int v = e.first, + w = e.second; + + if (f[s][v] > f[s][u] + w) { + f[s][v] = f[s][u] + w; + + if (!vis[v]) { + q.emplace(v); + vis[v] = true; + } + } + } + } + } + + for (int i = 1; i <= k; i++) { + ans = std::min(ans, f[(1 << k) - 1][a[i]]); + } + + cout << ans << endl; + + return 0; +}