From 669a58d86822a7d5646e3d3f2d0d4db80790cd3e Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 14 Mar 2023 06:28:02 +0800 Subject: [PATCH] =?UTF-8?q?#1975.=20=E3=80=90=E6=B4=9B=E8=B0=B7P4822?= =?UTF-8?q?=E3=80=91[BJWC2012]=E5=86=BB=E7=BB=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/73279 --- S2OJ/1975/1975.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 S2OJ/1975/1975.cpp diff --git a/S2OJ/1975/1975.cpp b/S2OJ/1975/1975.cpp new file mode 100644 index 00000000..1a263bc8 --- /dev/null +++ b/S2OJ/1975/1975.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 55; + +int n, m, k, dist[N << 6]; +std::vector> g[N << 6]; +bool vis[N << 6]; + +void dijkstra() { + memset(dist, 0x3f, sizeof(dist)); + + std::priority_queue, std::vector>, std::greater>> q; + + q.push(std::make_pair(0, 1)); + dist[1] = 0; + + while (!q.empty()) { + auto t = q.top(); + q.pop(); + + if (vis[t.second]) continue; + + for (auto e : g[t.second]) { + int v = e.first, + w = e.second; + + if (dist[v] > t.first + w) { + dist[v] = t.first + w; + + q.push(std::make_pair(dist[v], v)); + } + } + + vis[t.second] = true; + } +} + +int main() { + std::ios::sync_with_stdio(false); + + cin >> n >> m >> k; + + for (int i = 1, u, v, w; i <= m; i++) { + cin >> u >> v >> w; + + g[u].push_back(std::make_pair(v, w)); + g[v].push_back(std::make_pair(u, w)); + + for (int j = 1; j <= k; j++) { + g[u + (j - 1) * n].push_back(std::make_pair(v + j * n, w >> 1)); + g[v + (j - 1) * n].push_back(std::make_pair(u + j * n, w >> 1)); + g[u + j * n].push_back(std::make_pair(v + j * n, w)); + g[v + j * n].push_back(std::make_pair(u + j * n, w)); + } + } + + for (int i = 0; i <= k; i++) { + g[i * n].push_back(std::make_pair(i * n + n, 0)); + } + + dijkstra(); + + cout << dist[n * k + n] << endl; + + return 0; +}