题解:P1713 麦当劳叔叔的难题
Big__Shark · · 题解
提示:本题是一道很简单的水黑题,不能用搜索,因为搜索过不了,本题的方法就是 DP,提示很多了吧,自己做一下,如果还不会,看下面的题解!
方法 1: 从有哨兵的图到原图只有两个地方能过去,也就是原图起点的左边和原图终点的下面,这样围成的回路我们能保证一定经过原图的起点和终点,也就是我们的路径中有一段全是哨兵,而另一段就是最长路。求最长路只需减掉哨兵就好。
方法 2: 先补一条与别的路不相交的路进去,再用插头 DP 求回路。我们补了一条路后,我们发现原矩阵中的最长路和最短路之差等于新矩阵的最长回路和最短回路之差。所以我们只需储存当前插头状态的最大值和最小值,到边界时取最大和最小就可以了。
那么我们发现现在其实就是插头 DP 的板子了,板子指路,不过我们把求方案数改成了求路径长度。也很简单,我们不存方案数存路径长度,转移时取最大的而不是相加即可。
如果不会插头 DP 就参考插入DP教程学习一下
#include <bits/stdc++.h>
using namespace std;
const int N = 11810, M = 23007;
int n, m;
int g[20][20];
int h[2][M], q[2][N], cnt[2];
int v[2][M];
bool vis[20][20];
int dx[] = {0, 1, -1, 0}, dy[] = {1, 0, 0, -1};
struct node {
int x, y, step;
};
inline int find(int cur, int x) {
int t = x % M;
while (h[cur][t] != -1 && h[cur][t] != x) {
if (++t == M) t = 0;
}
return t;
}
inline void insert(int cur, int state, int w) {
int t = find(cur, state);
if (h[cur][t] == -1) {
h[cur][t] = state;
v[cur][t] = w;
q[cur][++cnt[cur]] = t;
} else {
v[cur][t] = max(w, v[cur][t]);
}
}
inline int get(int state, int k) {
return (state >> (k * 2)) & 3;
}
inline int build(int k, int x) {
return x * (1 << (k * 2));
}
inline int get_max() {
int res = -1e8;
int cur = 0;
memset(h, -1, sizeof h);
insert(cur, 0, 1);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= cnt[cur]; j++) {
h[cur][q[cur][j]] <<= 2;
}
for (int j = 1; j <= n; j++) {
int last = cur;
cur ^= 1;
cnt[cur] = 0;
memset(h[cur], -1, sizeof h[cur]);
for (int k = 1; k <= cnt[last]; k++) {
int state = h[last][q[last][k]], w = v[last][q[last][k]];
int x = get(state, j - 1), y = get(state, j);
if (!g[i][j]) {
insert(cur, state, w);
}
else if (!x && !y) {
insert(cur, state, w);
if (g[i + 1][j] && g[i][j + 1]) {
insert(cur, state + build(j - 1, 1) + build(j, 2), w + 1);
}
}
else if (!x && y) {
if (g[i][j + 1]) insert(cur, state, w + 1);
if (g[i + 1][j]) insert(cur, state - build(j, y) + build(j - 1, y), w + 1);
}
else if (x && !y) {
if (g[i + 1][j]) insert(cur, state, w + 1);
if (g[i][j + 1]) insert(cur, state - build(j - 1, x) + build(j, x), w + 1);
}
else if (x == 1 && y == 1) {
for (int u = j + 1, s = 1;; u++) {
int z = get(state, u);
if (z == 1) s++;
else if (z == 2) {
if (--s == 0) {
insert(cur, state - build(j, y) - build(j - 1, x) - build(u, 1), w + 1);
break;
}
}
}
}
else if (x == 2 && y == 2) {
for (int u = j - 2, s = 1;; u--) {
int z = get(state, u);
if (z == 2) s++;
else if (z == 1) {
if (--s == 0) {
insert(cur, state - build(j, y) - build(j - 1, x) + build(u, 1), w + 1);
break;
}
}
}
}
else if (x == 2 && y == 1) {
insert(cur, state - build(j - 1, x) - build(j, y), w + 1);
}
else if (i == n && j == n) {
res = max(res, w);
}
}
}
}
return res - 2 * n - 1;
}
inline int get_min() {
queue<node> q;
q.push({1, 3, 1});
while (!q.empty()) {
int x = q.front().x, y = q.front().y, step = q.front().step;
q.pop();
if (x == n - 2 && y == n) {
return step;
}
vis[x][y] = 1;
for (int i = 0; i < 4; i++) {
int xx = dx[i] + x, yy = dy[i] + y;
if (xx >= 1 && xx <= n - 2 && yy >= 3 && yy <= n && g[xx][yy] && !vis[xx][yy]) {
q.push({xx, yy, step + 1});
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
n += 2;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
g[i][j] = 1;
}
}
for (int i = 2; i <= n - 1; i++) {
g[i][2] = 0;
g[2][i] = 0;
}
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
g[a + 2][b + 2] = 0;
}
for (int i = 1; i <= n / 2; i++) {
int tmp[20];
memcpy(tmp, g[i], sizeof g[i]);
memcpy(g[i], g[n - i + 1], sizeof g[i]);
memcpy(g[n - i + 1], tmp, sizeof tmp);
}
int res_max = get_max(), res_min = get_min();
cout << res_max - res_min << '\n';
return 0;
}
题解仅供学习参考使用
抄袭、复制题解,以达到刷 AC 率/AC 数量或其他目的的行为,在洛谷是严格禁止的。
洛谷非常重视学术诚信。此类行为将会导致您成为作弊者。 具体细则请查看洛谷社区规则