From 65fadfdd85f9a745ad6c5c57739b7b4b06d346a9 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 12 May 2022 21:32:16 +0800 Subject: [PATCH] =?UTF-8?q?P4568=20[JLOI2011]=20=E9=A3=9E=E8=A1=8C?= =?UTF-8?q?=E8=B7=AF=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/75602917 --- Luogu/P4568/P4568.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Luogu/P4568/P4568.cpp diff --git a/Luogu/P4568/P4568.cpp b/Luogu/P4568/P4568.cpp new file mode 100644 index 00000000..69d679a0 --- /dev/null +++ b/Luogu/P4568/P4568.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 10005; + +int n, m, k, s, t; +std::vector> g[N << 4]; + +int dist[N << 4]; +bool vis[N << 4]; + +void dijkstra() { + memset(dist, 0x3f, sizeof(dist)); + memset(vis, 0x00, sizeof(vis)); + + std::priority_queue, std::vector>, std::greater>> q; + + q.push(std::make_pair(0, s)); + dist[s] = 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 >> s >> t; + + 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, 0)); + g[v + (j - 1) * n].push_back(std::make_pair(u + j * n, 0)); + 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 = 1; i <= k; i++) { + g[t + (i - 1) * n].push_back(std::make_pair(t + i * n, 0)); + } + + dijkstra(); + + cout << dist[t + n * k] << endl; + + return 0; +}