跳到正文

题解

题解 P2403 【[SDOI2010]所驼门王的宝藏】

yingjz约 7 分钟
原文
文章信息与版本

文章信息

发布
2019年5月14日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 7 分钟
题目
P2403

版本

  1. v2当前
  2. v1

思路

如果我们能把整张图(R×CR \times C)开出来的话那么问题就转化为:给出一个有向图,求最多一次能经过的点的数量,其中点可以重复经过。

由于点可以重复经过所以可以考虑缩点,缩点以后直接在 DAG 上 DPDP 即可,方程:

dp[v]=max⁡{dp[u]+val[v]}dp[v] = \max\{dp[u] + val[v]\}

其中 val[v]val[v] 表示 vv 这个强连通分量的点的数量。

那么现在的问题就是由于 R,CR, C 非常大,边的数量非常大(边数 O(n2)O(n^2)),不能直接存下。边的数量的瓶颈在于“横天门”和“纵寰门”,所以一个常规的思路对于每行和每列建一个特殊的点,这样就不用点到点的连边,边的数量降到了 O(n)O(n)。

(这是一个例子)

luo2403.png

实现

11 ~ RR 是每一行的特殊点;

R+1R+1 ~ R+CR + C 是每一列的特殊点;

R+C+1R+C+1 ~ R+C+nR + C + n 是藏宝宫室。

C++
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int N = 100007, T = 2100007, M = 1000007;
const int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int n, r, c, t, edc, edu[M], edv[M];
int ecnt, head[T], nxt[M], vet[M];
int qhead, qtail, que[T], f[T], in[T];
int stac[T], top, val[T], col[T], stamp, dfn[T], low[T], cnt;
bool instac[T];
struct Node {
	int x, y, t;
	bool operator <(const Node &ano) const {
		return x < ano.x || x == ano.x && y < ano.y;
	}
} a[N];

inline void add(int u, int v) {
	edu[++edc] = u; edv[edc] = v;

	vet[++ecnt] = v; nxt[ecnt] = head[u];
	head[u] = ecnt;
}

inline void eadd(int u, int v) {
	vet[++ecnt] = v; nxt[ecnt] = head[u];
	head[u] = ecnt;
}

void tarjan(int u) {
	dfn[u] = low[u] = ++stamp;
	stac[++top] = u; instac[u] = true;
	for (int e = head[u]; e; e = nxt[e]) {
		int v = vet[e];
		if (!dfn[v]) {
			tarjan(v); low[u] = min(low[u], low[v]);
		} else if (instac[v])
			low[u] = min(low[u], dfn[v]);
	}
	if (dfn[u] == low[u]) {
		col[u] = ++cnt; val[cnt] = u > r + c;
		while (stac[top] != u) {
			col[stac[top]] = cnt;
			val[cnt] += (stac[top] > r + c);
			instac[stac[top--]] = false;
		}
		instac[stac[top--]] = false;
	}
}

int getid(int x, int y) {
	int l = 1, r = n;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (a[mid].x == x && a[mid].y == y) return mid;
		else if (a[mid].x < x || a[mid].x == x && a[mid].y < y) l = mid + 1;
		else r = mid - 1;
	}
	return -1;
}

int main() {
	scanf("%d%d%d", &n, &r, &c);
	for (int i = 1; i <= n; ++i)
		scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].t);
	sort(a + 1, a + 1 + n);
	for (int i = 1; i <= n; ++i) {
		//处理额外点对当前点的连边
		add(a[i].x, r + c + i);
		add(r + a[i].y, r + c + i);
		//处理当前点对其它点的连边
		if (a[i].t == 1) add(r + c + i, a[i].x);
		else if (a[i].t == 2) add(r + c + i, r + a[i].y);
		else {
			for (int k = 0; k < 8; ++k) {
				int x = a[i].x + dx[k], y = a[i].y + dy[k];
				if (x >= 1 && x <= r && y >= 1 && y <= c) {
					int id = getid(x, y);
					if (id != -1) add(r + c + i, r + c + id);
				}
			}
		}
	}
	//缩点
	t = r + c + n;
	for (int i = 1; i <= t; ++i)
		if (!dfn[i]) tarjan(i);
	ecnt = 0; memset(head, 0, sizeof(head));
	for (int i = 1; i <= edc; ++i)
	 	if (col[edu[i]] != col[edv[i]]) {
		 	eadd(col[edu[i]], col[edv[i]]);
		 	++in[col[edv[i]]];
		 }
	//拓扑排序
	qhead = 0; qtail = -1;
	for (int i = 1; i <= cnt; ++i)
		if (!in[i]) {
			que[++qtail] = i;
			f[i] = val[i];
		}
	while (qhead <= qtail) {
		int u = que[qhead++];
		for (int e = head[u]; e; e = nxt[e]) {
			int v = vet[e];
			f[v] = max(f[v], f[u] + val[v]);
			if (--in[v] == 0) que[++qtail] = v;
		}
	}
	//统计答案
	int ans = 0;
	for (int i = 1; i <= cnt; ++i)
		ans = max(ans, f[i]);
	printf("%d\n", ans);
	return 0;
}