跳到正文

题解

题解:P13648 [NOISG 2016] Rock Climbing

Register_int约 4 分钟
原文

这是 2026年1月17日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年1月17日
更新
2026年1月18日
篇幅
约 4 分钟
题目
P13648

版本

  1. v2当前
  2. v1

何意味?

管他题目条件是啥,反正图建出来是个 dag,让你求他传递闭包的最大独立集。

那还说啥了?最小链覆盖跑一下没了啊?时间复杂度 O(n3/w+n2.5)\mathcal{O}(n^3/w+n^{2.5})。

C++
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 1e3 + 10;
const int MAXM = 2e5 + 10;
const int inf = 0x3f3f3f3f;

struct node {
	int v, c, nxt;
	node(int v = 0, int c = 0, int nxt = 0) : v(v), c(c), nxt(nxt) {}
} e[MAXM << 1]; int head[MAXN], tot = 1;

inline 
void add(int u, int v, int c) {
	e[++tot] = node(v, c, head[u]), head[u] = tot;
	e[++tot] = node(u, 0, head[v]), head[v] = tot;
}

int lvl[MAXN], lst[MAXN];

inline 
bool bfs(int s, int t) {
	memset(lvl, 0xff, sizeof lvl);
	queue<int> q; q.emplace(s), lvl[s] = 0;
	for (int u; !q.empty(); ) {
		u = q.front(), q.pop(), lst[u] = head[u];
		for (int i = head[u]; i; i = e[i].nxt) {
			int v = e[i].v, c = e[i].c;
			if (!~lvl[v] && c) lvl[v] = lvl[u] + 1, q.emplace(v);
		}
	}
	return ~lvl[t];
}

inline 
int dfs(int u, int t, int f) {
	if (u == t) return f; int res = 0;
	for (int &i = lst[u]; i; i = e[i].nxt) {
		int v = e[i].v, c = e[i].c;
		if (!c || lvl[v] != lvl[u] + 1) continue;
		int x = dfs(v, t, min(f, c));
		e[i].c -= x, e[i ^ 1].c += x, f -= x, res += x;
	}
	return res;
}

inline 
int dinic(int s, int t) {
	int res = 0;
	for (; bfs(s, t); res += dfs(s, t, inf));
	return res;
}

int n, s, t, x[MAXN], y[MAXN], w[MAXN];

bitset<MAXN> f[MAXN];

int main() {
	scanf("%d", &n), t = n << 1 | 1;
	for (int i = 1; i <= n; i++) scanf("%d%d%d", &x[i], &y[i], &w[i]);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (i == j || y[i] >= y[j]) continue;
			if (max(abs(x[i] - x[j]), abs(y[i] - y[j])) <= max(w[i], w[j])) f[i].set(j);
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) if (f[j][i]) f[j] |= f[i];
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) if (f[i][j]) add(i, n + j, 1); 
	}
	for (int i = 1; i <= n; i++) add(s, i, 1), add(n + i, t, 1);
	int res = n - dinic(s, t);
	printf("%d", res == n ? -1 : res + 1);
}