题解:P14912 [NHSPC 2024] 海盗地图

· · 题解

非常神秘的题。

做法就随机选 2000 个点,对这些点跑最短路,然后对于每一次询问查询一下最小值。

证明: 令当前随机了 x 个点,正确率为 p,那么 p = (1 - (1 - \frac{1}{\sqrt{n}})^x) ^ q。

下面来解释,以下路径为从 s_i 到 t_i 用时最短的路径。

对于每一次询问,因为路径长度大于 \sqrt{n},所以任意一个节点 i 的是路径的一点的概率为 \frac{1}{\sqrt{n}},换句话说,这个节点不在路径上的概率为 1 - \frac{1}{\sqrt{n}},所以选 x 个几点,这些节点都不在路径上的概率为 (1 - \frac{1}{\sqrt{n}})^x,反之这些节点中有在路径上的点的概率为 1 - (1 - \frac{1}{\sqrt{n}})^x。 ::::info[这里为什么不能是 (\frac{1}{\sqrt{n}}) ^ x] 这个公式求的是都在这条路径上的概率,而这道题只需要一个点在路径上面就可以求出答案。 :::: 这样答对这道题的概率就是 (1 - (1 - \frac{1}{\sqrt{n}})^x)^q。 ::::info[code]

#include<bits/stdc++.h>
const int MAXN = 1e4 + 5;
#define pb push_back
typedef long long LL;
const LL inf = 1e15;
int n, m, q;
std::vector<int> e[MAXN];
inline void add(int u, int v)
{ e[u].pb(v);}
struct DIJ
{
    LL dis[MAXN];
    inline void dij(int s)
    {
        for(int i = 1; i <= n; i++)
            dis[i] = inf;
        dis[s] = 0;
        std::queue<int> q;
        q.push(s);
        while(q.size())
        {
            int u = q.front();
            q.pop();
            for(int v : e[u])
                if(dis[v] == inf)
                {
                    dis[v] = dis[u] + 1;
                    q.push(v);
                }
        }
    }
}T[2005];
int id[MAXN], tot;
int main()
{
    srand(time(0));
    scanf("%d%d%d", &n, &m, &q);
    for(int _ = 1, u, v; _ <= m; _++)
    {
        scanf("%d%d", &u, &v);
        add(u, v), add(v, u);
    } tot = std::min(n, 2000);
    for(int i = 1; i <= tot; i++)
        id[i] = rand() % n + 1;
    for(int i = 1; i <= tot; i++)
        T[i].dij(id[i]);
    for(int _ = 1, s, t; _ <= q; _++)
    {
        scanf("%d%d", &s, &t);
        LL ans = inf;
        for(int i = 1; i <= tot; i++)
            ans = std::min(ans, T[i].dis[s] + T[i].dis[t]);
        printf("%lld%c", ans, (_ == q ? '\n' : ' '));
    } return 0;
}

::::