yaoxi-std 的博客

$\text{开}\mathop{\text{卷}}\limits^{ju\check{a}n}\text{有益}$

0%

P4011 孤岛营救问题

P4011 孤岛营救问题

题面

题目链接

解法

显然的状压分层$BFS$。

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* @file: P4011.cpp
* @author: yaoxi-std
* @url: https://www.luogu.com.cn/problem/P4011
*/
// #pragma GCC optimize ("O2")
// #pragma GCC optimize ("Ofast", "inline", "-ffast-math")
// #pragma GCC target ("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define resetIO(x) \
freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define debug(fmt, ...) \
fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
template <class _Tp>
inline _Tp& read(_Tp &x) {
bool sign = false;
char ch = getchar();
long double tmp = 1;
for (; !isdigit(ch); ch = getchar())
sign |= (ch == '-');
for (x = 0; isdigit(ch); ch = getchar())
x = x * 10 + (ch ^ 48);
if (ch == '.')
for (ch = getchar(); isdigit(ch); ch = getchar())
tmp /= 10.0, x += tmp * (ch ^ 48);
return sign ? (x = -x) : x;
}
template <class _Tp>
inline void write(_Tp x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar((x % 10) ^ 48);
}
const int MAXN = 15;
const int MAXK = 1 << 11;
const int INF = 0x3f3f3f3f3f3f3f3f;
const int DX[4] = {-1, 0, 0, 1};
const int DY[4] = {0, -1, 1, 0};
struct node {
int x, y, s;
};
int n, m, p, k, s, a[MAXN][MAXN], dis[MAXN][MAXN][MAXK], g[MAXN][MAXN][MAXN][MAXN];
signed main() {
read(n), read(m), read(p);
read(k);
for (int i = 1; i <= k; ++i) {
int tx1, ty1, tx2, ty2, tg;
read(tx1), read(ty1), read(tx2), read(ty2), read(tg);
g[tx1][ty1][tx2][ty2] |= 1 << tg;
g[tx2][ty2][tx1][ty1] |= 1 << tg;
}
read(s);
for (int i = 1; i <= s; ++i) {
int tx, ty, tq;
read(tx), read(ty), read(tq);
a[tx][ty] |= 1 << tq;
}
memset(dis, -1, sizeof(dis));
queue<node> que;
que.push({1, 1, 0});
dis[1][1][0] = 0;
int ans = INF;
while (!que.empty()) {
node t = que.front();
que.pop();
int x = t.x, y = t.y, s = t.s;
if (x == n && y == m)
ans = min(ans, dis[x][y][s]);
for (int i = 0; i < 4; ++i) {
int tx = x + DX[i];
int ty = y + DY[i];
if (tx < 1 || ty < 1 || tx > n || ty > m)
continue;
if ((g[x][y][tx][ty] & s) != g[x][y][tx][ty])
continue;
int ts = s | a[tx][ty];
if (dis[tx][ty][ts] != -1)
continue;
dis[tx][ty][ts] = dis[x][y][s] + 1;
que.push({tx, ty, ts});
}
}
write(ans == INF ? -1 : ans), putchar('\n');
return 0;
}