From d3eb18bfddb90192f64faafe73324161debfdc45 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 24 Jul 2022 20:45:05 +0800 Subject: [PATCH] =?UTF-8?q?P3645=20[APIO2015]=20=E9=9B=85=E5=8A=A0?= =?UTF-8?q?=E8=BE=BE=E7=9A=84=E6=91=A9=E5=A4=A9=E6=A5=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/81011789 --- Luogu/P3645/P3645.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Luogu/P3645/P3645.cpp diff --git a/Luogu/P3645/P3645.cpp b/Luogu/P3645/P3645.cpp new file mode 100644 index 00000000..cd3a8fd2 --- /dev/null +++ b/Luogu/P3645/P3645.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 30001; + +int n, m, l, s, t; +std::vector> g[N * 101]; +int dist[N * 101]; +std::bitset vis; + +int spfa() { + memset(dist, 0x3f, sizeof(dist)); + std::queue q; + + dist[s] = 0; + q.push(s); + vis[s] = true; + + while (!q.empty()) { + int u = q.front(); + q.pop(); + + vis[u] = false; + + for (auto t : g[u]) { + int v = t.first; + short w = t.second; + + if (dist[v] > dist[u] + w) { + dist[v] = dist[u] + w; + + if (!vis[v]) { + q.push(v); + vis[v] = true; + } + } + } + } + + return dist[t] == 0x3f3f3f3f ? -1 : dist[t]; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + l = std::min(static_cast(std::sqrt(n)), 100); + + for (int i = 1, b, p; i <= m; i++) { + cin >> b >> p; + + b++; + + if (i == 1) s = b; + if (i == 2) t = b; + + if (p <= l) { + g[b].emplace_back(p * n + b, 0); + } else { + for (int j = 1; b + j * p <= n; j++) { + g[b].emplace_back(b + j * p, j); + } + + for (int j = 1; b - j * p > 0; j++) { + g[b].emplace_back(b - j * p, j); + } + } + } + + for (int i = 1; i <= l; i++) { + for (int j = 1; j <= n; j++) { + g[i * n + j].emplace_back(j, 0); + } + + for (int j = 1; j <= n - i; j++) { + g[i * n + j].emplace_back(i * n + j + i, 1); + g[i * n + j + i].emplace_back(i * n + j, 1); + } + } + + cout << spfa() << endl; + + return 0; +}