题解:P12220 [蓝桥杯 2023 国 Java B] 星球

· · 题解

提供一种搜索的写法

看到这道题的第一眼,我就注意到数据范围里写着 n \le 18。一眼丁真状压DP,一眼 DFS!

先拿部分分:题目对于 20\% 的数据,n 最多只有 8。这就简单了,直接用全排列的暴力 DFS 就能拿到这 20 分。

写代码的时候要注意,计算两个星球之间的距离得用欧几里得距离公式。代码里的 double vecdist(const node& a, const node& b) 函数就是这个公式,公式长这样:

d(A, B) = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2}

想了解更多的话,可以去看看 距离 - OI Wiki。

代码实现:

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 18;
double INF = 1e18;

int n;
bool st[20];
double min_energy = INF;
vector<int> path;

inline long long read(){register long long x=0;register char ch=getchar();register bool f=0;while(!isdigit(ch))f|=ch=='-',ch=getchar();while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();return f?-x:x;}inline void write(long long x){if(x<0){x=-x;putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');}

struct node {
    double x, y, z, w;
} pl[20];

double vecdist(const node& a, const node& b) {
    return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) + (a.z - b.z)*(a.z - b.z));
}

void dfs(int cnt, int cur, double energy) {
    if (cnt == n) {
        min_energy = min(min_energy, energy);
        return;
    }

    for (int i = 0; i < n; ++i) {
        if (!st[i]) {
            st[i] = 1;
            if (cnt == 0) {
                dfs(cnt + 1, i, 0.0);
            } else {
                double d = vecdist(pl[cur], pl[i]);
                dfs(cnt + 1, i, energy + d * pl[i].w);
            }
            st[i] = 0;
        }
    }
}

int main() {
    n = read();
    for (int i = 0; i < n; ++i) pl[i].x = read(), pl[i].y = read(), pl[i].z = read(), pl[i].w = read();

    memset(st, 0, sizeof(st));
    dfs(0, -1, 0);

    printf("%.2lf\n", min_energy);

    return 0;
}

20 分到手!

不过暴力的缺陷也很明显——它的时间复杂度是 O(n!)。当 n 超过 11 之后直接就炸了。。。

那怎么办呢?别急,我们可以用记忆化搜索来优化。

思路概述:

其实这道题本质上和带权的旅行商问题有点像——我们要找一条路径,让总能量 E 最小。这个总能量怎么算呢?就是从第二个星球开始,每个星球到前一个星球的距离,乘以当前星球的防御强度,最后把这些值加起来:

E = \sum_{i=2}^{n} \underbrace{d(p_{i-1}, p_i)}_{\text{两星球间距离}} \times \underbrace{w_i}_{\text{当前星球防御强度}}

具体怎么做记忆化搜索呢?我想了个办法:

代码实现

#include <bits/stdc++.h>
using namespace std;
int n;
double memo[262145][20];// 262144 其实就是 2 的 18 次方,刚好能存下 n=18 时的状态
bool st[20];

inline long long read(){register long long x=0;register char ch=getchar();register bool f=0;while(!isdigit(ch))f|=ch=='-',ch=getchar();while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();return f?-x:x;}inline void write(long long x){if(x<0){x=-x;putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');}

struct node {double x, y, z, w;} pl[20];

double vecdist(const node& a, const node& b) {
    return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) + (a.z - b.z)*(a.z - b.z));
}

// 返回:当前在 cur 节点,已访问 mask 的最小能量
double dfs(int mask, int cur) {
    if (mask == (1 << n) - 1) return 0.0;

    if (memo[mask][cur] >= 0) return memo[mask][cur];

    double res = 1e18;
    for (int i = 0; i < n; ++i) {
        if ((mask & (1 << i)) == 0) {
            double d = vecdist(pl[cur], pl[i]);
            res = min(res, dfs(mask | (1 << i), i) + d * pl[i].w);
        }
    }

    return memo[mask][cur] = res;
}

int main() {
    n = read();
    for (int i = 0; i < n; ++i)
        pl[i].x = read(), pl[i].y = read(), pl[i].z = read(), pl[i].w = read();

    double ans = 1e18;
    memset(memo, -1, sizeof(memo)); // 先把记忆化数组初始化成 -1,标记没访问过

    for (int i = 0; i < n; ++i) {
        double tmp = dfs(1 << i, i); // 从每个星球开始试试
        ans = min(ans, tmp);
    }

    printf("%.2lf\n", ans);

    return 0;
}

这种记忆化搜索的时间复杂度是 O(n \times 2^n)。当 n=18 时,算出来大概是 18 \times 262144,也就四百多万次运算,完全够用。而且这种思路和状压 dp 的想法其实差不多,效率自然不用担心。