跳到正文

题解

P11535 题解

huangrenheluogu约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年7月22日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 7 分钟
获赞
1
题目
P11535

版本

  1. v1当前

放 NOIP 联考 T1 太需要观察了吧。

我们充分发扬人类智慧,有一个暴力的做法。从最开始使得图联通的值 vv 开始枚举最大值,然后把所有 ≤v\le v 的点加入,之后就可以跑最短路,当前点的高度应该是 min⁡(distx,v)\min(dist_x,v)。对于下降部分,从 nn 开始跑一次即可。

当 vv 增大的时候,相当于是激活若干个点,以这些点为源点进行 Dijkstra 尝试更新其他点即可。

复杂度上界是 O(nmlog⁡n)\mathcal{O}(nm\log n) 的,但是直接过了。不知道为什么,希望得到正确性证明或 hack。

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, inf = 0x3f3f3f3f;
int n, m, u, v, a[N], fa[N], dist[2][N], id[N], val, p, ans = inf;
vector<int> G[N];
struct node {
    int x, dist;
    node() {}
    node(int _x, int _dist) {
        x = _x, dist = _dist;
    }
}tp;
inline bool operator < (node x, node y) {
    return x.dist > y.dist;
}
priority_queue<node> qu;
inline int getfa(int x) {
    return fa[x] == x ? x : fa[x] = getfa(fa[x]);
}
inline int max(int x, int y) {
    return x > y ? x : y;
}
bool vis[N];
vector<int> okk;
inline void bfs(int st, int* dist) {
    for(int i = 1; i <= n; i++) dist[i] = inf;
    dist[st] = 0;
    qu.push(node(st, 0));
    while(!qu.empty()) {
        tp = qu.top(), qu.pop();
        if(dist[tp.x] ^ tp.dist) continue ;
        for(int y : G[tp.x]) if(a[y] <= val && dist[y] > max(dist[tp.x] + 1, a[y])) {
            dist[y] = max(dist[tp.x] + 1, a[y]);
            qu.push(node(y, dist[y]));
        }
    }
}
inline void dijk(vector<int> &vc, int* dist) {
    for(int o : vc) {
        for(int y : G[o]) if(a[y] <= val && dist[o] > max(dist[y] + 1, a[o])) {
            dist[o] = max(dist[y] + 1, a[o]);
        }
        if(dist[o] < inf) {
            qu.push(node(o, dist[o]));
        }
    }
    while(!qu.empty()) {
        tp = qu.top(), qu.pop();
        if(dist[tp.x] ^ tp.dist) continue ;
        if(!vis[tp.x]) {
            okk.push_back(tp.x), vis[tp.x] = true;
        }
        for(int y : G[tp.x]) if(a[y] <= val && dist[y] > max(dist[tp.x] + 1, a[y])) {
            dist[y] = max(dist[tp.x] + 1, a[y]);
            qu.push(node(y, dist[y]));
        }
    }
}
int main() {
    // freopen("star.in", "r", stdin);
    // freopen("star.out", "w", stdout);
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for(int i = 1; i <= m; i++) {
        scanf("%d%d", &u, &v);
        G[u].push_back(v), G[v].push_back(u);
    }
    for(int i = 1; i <= n; i++) fa[i] = i, id[i] = i;
    sort(id + 1, id + n + 1, [&](int x, int y) {
        return a[x] < a[y];
    });
    for(int i = 1, x; i <= n; i++) {
        x = id[i];
        for(int y : G[x]) if(a[y] <= a[x]) {
            fa[getfa(x)] = getfa(y);
        }
        if(getfa(1) == getfa(n) && a[x] >= a[1] && a[x] >= a[n]) {
            val = a[x];break ;
        }
    }
    bfs(1, dist[0]);
    bfs(n, dist[1]);
    for(int i = 1; i <= n; i++) if(a[i] <= val) {
        ans = min(ans, max(dist[0][i], val) + max(dist[1][i], val));
    }
    p = 0;
    while(p < n && a[id[p + 1]] <= val) p++;
    vector<int> vc;
    while(p < n) {
        p++;
        val = a[id[p]];
        if(2 * val > ans) break ;
        vc.push_back(id[p]);
        while(p < n && a[id[p + 1]] == val) p++, vc.push_back(id[p]);
        dijk(vc, dist[0]);
        dijk(vc, dist[1]);
        vc.clear();
        for(int o : okk) {
            ans = min(ans, max(dist[0][o], val) + max(dist[1][o], val));
        }
        okk.clear();
    }
    printf("%d\n", ans);
    return 0;
}
原文