标签:define size namespace for back def count clu cond
https://www.acwing.com/problem/content/1133/
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define x first
#define y second
#define inf 0x3f3f3f3f
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int n, m, k, p;
const int N = 11, M = 360, P = 1 << 10;
int h[N * N], ne[M], e[M], w[M], idx;
int g[N][N], key[N * N];
int dist[N * N][P];
bool st[N * N][P];
set<PII> edges;
void add(int a, int b, int c) {
ne[idx] = h[a], e[idx] = b, w[idx] = c, h[a] = idx++;
}
void bulid() {
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
for (int u = 0; u < 4; ++u) {
int x = i + dx[u], y = j + dy[u];
if (!x || x > n || !y || y > m) continue;
int a = g[i][j], b = g[x][y];
if (!edges.count({a, b})) add(a, b, 0);
}
}
int bfs() {
memset(dist, 0x3f, sizeof dist);
dist[1][0] = 0;
deque<PII> q;
q.pb({1, 0});
while (q.size()) {
PII t = q.front();
q.pop_front();
if (st[t.x][t.y]) continue;
st[t.x][t.y] = true;
if (t.x == n * m) return dist[t.x][t.y];
if (key[t.x]) {
int state = t.y | key[t.x];
if (dist[t.x][state] > dist[t.x][t.y]) {
dist[t.x][state] = dist[t.x][t.y];
q.push_front({t.x, state});
}
}
for (int i = h[t.x]; ~i; i = ne[i]) {
int j = e[i];
if (w[i] && !(t.y >> w[i] - 1 & 1)) continue;
if (dist[j][t.y] > dist[t.x][t.y] + 1) {
dist[j][t.y] = dist[t.x][t.y] + 1;
q.push_back({j, t.y});
}
}
}
return -1;
}
int main() {
IO;
cin >> n >> m >> p >> k;
for (int i = 1, t = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
g[i][j] = t++;
memset(h, -1, sizeof h);
while (k--) {
int x1, y1, x2, y2, c;
cin >> x1 >> y1 >> x2 >> y2 >> c;
int a = g[x1][y1], b = g[x2][y2];
edges.insert({a, b}), edges.insert({b, a});
if (c) add(a, b, c), add(b, a, c);
}
bulid();
int s;
cin >> s;
while (s--) {
int x, y, c;
cin >> x >> y >> c;
key[g[x][y]] |= 1 << c - 1;
}
cout << bfs() << ‘\n‘;
return 0;
}
标签:define size namespace for back def count clu cond
原文地址:https://www.cnblogs.com/phr2000/p/14396114.html