题解 [模拟赛 2022.11.14] Easy

· · 个人记录

贪心地,若某天能吃我们指定的某条 1 \sim n 路径上的 boss 我们就一定要吃。

那假如某一天我们吃不了了呢?若 a \leq b,此后你一定吃不了任何 boss 了;若 a > b 你也许还可以吃路径外的 boss。

从这个角度出发,先来讨论 a \leq b。由上面的结论,在你还能吃 boss 时,一定是每天吃一个。

注意到我们能否吃掉某个 boss 这个事件是有一个分界点的,即我们在 1 \sim \operatorname{lim}_i 天内可以吃第 i 个 boss,之后不能吃。

也就是说,我们需要找到从 1 到 n 的一条路径 P,使得 \forall 1 \leq i \leq |P|, i < \operatorname{lim}_{P_i}。直接 bfs 求带该限制的边权为 1 的最短路即可。

接下来讨论 a > b,注意到我们可以类似地求出一个 \operatorname{lim}_i,即我们在 \geq \operatorname{lim}_i 天内可以吃第 i 个 boss,之前不能吃。

现在我们仿照 a \leq b 的情况求出一个最短路。不过在边权为 1 的基础上,每个 dis_i 要跟 \operatorname{lim}_i 取 \max——因为最佳情况下至少此时你才可以经过这个点。于是这里我们需要像 Dijkstra 一样用优先队列维护。

现在,让我们来证明这个人类智慧结论:

充分性:假定 dis_n < x,考虑当前要走第 i 步,随意挑选一个 dis_u = i 的 u,如果可以走到 u,走它即可;否则,一定存在一个 dis_u < i 的且没有走过的 u,依然走它即可。

必要性:假定 dis_n \geq x,注意到 x > 0(因为 0 显然不满足条件)。考虑 x - 1 满足条件,则 \displaystyle\sum_{i = 1}^n [dis_i < x] \geq x,但 \displaystyle\sum_{i = 1}^n [dis_i \leq x] \leq x,则在 x - 1 到 x 之间出现了 gap——即不存在一个 dis_u = x。于是显然无解。

对于这个结论,开个大小为 O(n + \max l_i) 的桶统计一下即可。时间复杂度为 O(\sum(n + m \log m + \max l_i))。

我们首先尝试给出**无解**的判定条件: - 考虑贪心,每天选一个 $\operatorname{lim}_i$ 最小的 boss 来打,boss 被打完或打不动时退出。 - 若最终可以打败 $n$ 处的 boss 则一定有解。 在有解的情况下,我们扔掉不能到的点,接着尝试给出**答案下界**: - 设 $dis_i$ 表示不考虑绕路情况下,最早第几天可以打败 $i$(这里的定义其实跟原版题解一致)。 - 转移为 $dis_v \leftarrow \min(dis_v, \max(dis_u, \operatorname{lim}_v) + 1)$,可以类似最短路地求出。 - 于是答案下界为 $dis_n$。 套路地,我们最后尝试说明**答案下界可以取到**: - 考虑 $dis_n$ 所给出的 $1 \to n$ 的路径,设路径上的点依次为 $P_0 = 1, P_1, P_2, \cdots, P_k = n$。 - 当我们准备走 $P_i$ 时,若当前天数不够,考虑一开始判无解的贪心,我们每次从堆中弹出 $\operatorname{lim}_j$ 最小的 boss 并更新,直至天数足够。 - 假定此时天数不够,则该点一定不属于一开始的贪心所求出的可达点,矛盾。 判断无解时开个堆贪心,求 $dis_i$ 时类似 Dijkstra 地用堆维护即可。 时间复杂度为 $O(T(n + m \log m))$。 代码: ```cpp #include <iostream> #include <queue> #include <cstdio> namespace Fread { const int BUFFER_SIZE = 1 << 25; char buffer[BUFFER_SIZE + 7]; char *pstart = buffer, *pend = buffer; inline char getchar(){ if (pstart < pend) return *pstart++; pstart = buffer; pend = buffer + fread(buffer, 1, BUFFER_SIZE, stdin); return pstart == pend ? EOF : *pstart++; } inline int read(){ int ans = 0; char ch = getchar(); while (ch < '0' || ch > '9'){ ch = getchar(); } while (ch >= '0' && ch <= '9'){ ans = ans * 10 + (ch ^ 48); ch = getchar(); } return ans; } } using namespace std; typedef struct { int nxt; int end; } Edge; typedef struct Node1_tag { int pos; Node1_tag(int id_){ pos = id_; } } Node1; typedef struct Node2_tag { int dis; int pos; Node2_tag(int dis_, int pos_){ dis = dis_; pos = pos_; } } Node2; int cnt; int head[200007], l[200007], lim[200007], dis[200007]; bool vis[200007]; Edge edge[400007]; queue<int> q; priority_queue<Node1> pq1; priority_queue<Node2> pq2; bool operator <(const Node1 a, const Node1 b){ return lim[a.pos] > lim[b.pos]; } bool operator <(const Node2 a, const Node2 b){ return a.dis > b.dis; } inline void init(int n){ cnt = 0; for (int i = 1; i <= n; i++){ head[i] = 0; dis[i] = 0x7fffffff; } } inline void add_edge(int start, int end){ cnt++; edge[cnt].nxt = head[start]; head[start] = cnt; edge[cnt].end = end; } inline int floor(int x, int y){ return x < 0 ? 0 : x / y; } inline int ceil(int x, int y){ return x <= 0 ? 0 : (x - 1) / y + 1; } int main(){ int t = Fread::read(); for (int i = 1; i <= t; i++){ int n = Fread::read(), m = Fread::read(), a = Fread::read(), b = Fread::read(); init(n); for (int j = 1; j <= m; j++){ int u = Fread::read(), v = Fread::read(); add_edge(u, v); add_edge(v, u); } for (int j = 1; j <= n; j++){ l[j] = Fread::read(); } if (a <= b){ for (int j = 2; j <= n; j++){ if (a == b){ lim[j] = l[1] - l[j] - b + 1 > 0 ? 0x7fffffff : -1; } else { lim[j] = floor(l[1] - l[j] - b + 1, b - a); } } dis[1] = 0; q.push(1); while (!q.empty()){ int cur = q.front(), dis_i = dis[cur] + 1; q.pop(); for (int j = head[cur]; j != 0; j = edge[j].nxt){ int x = edge[j].end; if (dis_i <= lim[x] && dis[x] == 0x7fffffff){ dis[x] = dis_i; q.push(x); } } } if (dis[n] == 0x7fffffff){ cout << -1 << endl; } else { cout << dis[n] << endl; } } else { int day = 0; while (!pq1.empty()) pq1.pop(); for (int j = 2; j <= n; j++){ lim[j] = ceil(l[j] + b - l[1] + 1, a - b); vis[j] = false; } vis[1] = true; for (int j = head[1]; j != 0; j = edge[j].nxt){ int x = edge[j].end; vis[x] = true; pq1.push(Node1(x)); } while (!pq1.empty()){ int cur = pq1.top().pos; pq1.pop(); if (day < lim[cur]) break; day++; for (int j = head[cur]; j != 0; j = edge[j].nxt){ int x = edge[j].end; if (!vis[x]){ vis[x] = true; pq1.push(Node1(x)); } } } for (int j = 1; j <= n; j++){ vis[j] = false; } dis[1] = 0; pq2.push(Node2(0, 1)); while (!pq2.empty()){ int cur = pq2.top().pos; pq2.pop(); if (vis[cur]) continue; vis[cur] = false; for (int j = head[cur]; j != 0; j = edge[j].nxt){ int x = edge[j].end, y = max(dis[cur], lim[x]) + 1; if (dis[x] > y){ dis[x] = y; pq2.push(Node2(y, x)); } } } if (dis[n] > day){ cout << -1 << endl; } else { cout << dis[n] << endl; } } } return 0; } ```