题解:P3888 [GDOI2014] 拯救莫莉斯

· · 题解

题意

link

题解

首先 n \times m \le 50,即 \min(n,m) \le \sqrt{50},显然矩阵转置不会改变答案所以令 n\le m,考虑状压 dp。

定义一行的状态为一个 01 二进制数,每一位表示是否选择建造油库,考虑需要存储到现在为止哪些行的状态。首先肯定需要上一行,如果一个点上面建造了油库那么这个点就可以不需要建造。对于左和右只需要存储当前行状态判断即可。

但是还有一个问题就是可以被下面的点所影响,所以我们考虑每枚举到一行枚举当前行,上一行和上上行的状态,每次转移保证上一行合法,而当前行不一定要保证合法,因为还可能被下面影响。而对于上一行,其上下左右的点都被存下来了。

所以令 f_{i,s,t} 表示考虑前 i 行,当前行状态为 s,上一行状态为 t 的最小代价,令 g_{i,s,t} 表示最小代价下的最小油库数量,有转移:

if (f[i-1][t][u] + cost < f[i][s][t]) f[i][s][t] = f[i-1][t][u] + cost, g[i][s][t] = g[i-1][t][u] + num;
else if (f[i-1][t][u] + cost == f[i][s][t] && g[i-1][t][u] + num < g[i][s][t]) g[i][s][t] = g[i-1][t][u] + num;

转移的时候需要保证 u,t,s 这三个状态连在一起合法,转移的代价和油库数一个需要预处理,一个就是 \operatorname{popcount}(s)。

最后统计答案的时候因为只保证了前 n-1 行合法,要判一下最后一行合法,答案再统计到 ans 中。

如果预处理是否合法和代价,时间复杂度 O(n2^{3m})。

Code

#include <bits/stdc++.h>
#define db double
#define int long long
#define ll long long
#define pc putchar
#define gc getchar
#define sz(x) ((int)x.size())
#define F(i, a, b) for (int i = (a); i <= (b); ++i)
#define D(i, a, b) for (int i = (a); i >= (b); --i)
#define TM cerr<<fabs(&Med-&Mbe)/1048576.0<<"MB "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms\n"
using namespace std;
bool Mbe;
inline ll rd() {
    ll x = 0; bool f = 1; char ch = gc();
    while (ch < '0' || ch > '9') { if (ch == '-') f = 0; ch = gc(); }
    while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();
    return (f ? x : (-x));
}
inline void wr(ll x) {
    if (x < 0) pc('-'), x = -x;
    if (x > 9) wr(x / 10);
    pc(x % 10 + '0');
}
int n, m, a[55][55], b[55][55], f[55][130][130], g[55][130][130], cc[55][130];
bool can[130][130][130];
bool check(int s, int t, int u) {
    F(i, 0, m-1) if (!(t >> i & 1)) {
        if ((i == 0 && !(t >> (i+1) & 1)) || (i == m && !(t >> (i-1) & 1)) || (!(t >> (i-1) & 1) && !(t >> (i+1) & 1))) {
            if (!(s >> i & 1) && !(u >> i & 1)) return 0; 
        }       
    }
    return 1;
}
bool check2(int t, int s) {
    F(i, 0, m-1) if (!(t >> i & 1)) {
        if ((i == 0 && !(t >> (i+1) & 1)) || (i == m && !(t >> (i-1) & 1)) || (!(t >> (i-1) & 1) && !(t >> (i+1) & 1))) {
            if (!(s >> i & 1)) return 0; 
        }       
    }
    return 1;
}
int calc(int i, int s) {
    int ans = 0;
    F(j, 0, m-1) if (s >> j & 1) ans += a[i][j+1];
    return ans;
}
bool Med;
signed main() {
    n = rd(), m = rd(); F(i, 1, n) F(j, 1, m) b[i][j] = rd();
    if (n < m) {
        F(i, 1, n) F(j, 1, m) a[j][i] = b[i][j];
        swap(n, m);
    } else {
        F(i, 1, n) F(j, 1, m) a[i][j] = b[i][j];
    }
    int full = (1 << m) - 1;
    F(s, 0, full) F(t, 0, full) F(u, 0, full) can[s][t][u] = check(s, t, u);
    F(i, 1, n) F(s, 0, full) {
        cc[i][s] = calc(i, s);
    }
    memset(f, 0x3f, sizeof f), memset(g, 0x3f, sizeof g);
    g[0][0][0] = 0, f[0][0][0] = 0;
    F(i, 1, n) {
        F(s, 0, full) {
            F(t, 0, full) {
                F(u, 0, full) {
                    if (i == 1 || can[s][t][u]) {
                        int cost = cc[i][s], num = __builtin_popcount(s);
                        if (f[i-1][t][u] + cost < f[i][s][t]) f[i][s][t] = f[i-1][t][u] + cost, g[i][s][t] = g[i-1][t][u] + num;
                        else if (f[i-1][t][u] + cost == f[i][s][t] && g[i-1][t][u] + num < g[i][s][t]) g[i][s][t] = g[i-1][t][u] + num;
                    }
                }
            }
        }
    }
    int ans = 0x3f3f3f3f3f3f3f3f, anf = 0;
    F(s, 0, full) F(t, 0, full) { 
        if (!check2(s, t)) continue;
        if (f[n][s][t] < ans) ans = f[n][s][t], anf = g[n][s][t];
        else if (f[n][s][t] == ans && g[n][s][t] < anf) anf = g[n][s][t];  
    }
    wr(anf), pc(' '), wr(ans), pc('\n');
    return 0;
}