【ZOJ 1654】Place the Robots(网络流)

Place the Robots

题目链接:ZOJ 1654

题目大意

给你一个网格图,然后有墙、空地和草地。
你可以放点在空地上,然后你要保证一个点它同一行同一列上的别的点都有一个墙隔开。
然后问你最多能放多少个点。

思路

首先不难想到这么一种做法:
直接看每个空地放点,如果两个空地不能都放点,就连边,然后跑最大独立集。
但是你会发现它不一定能弄成二分图,那一般图的最大独立集是 NP-Hard 不太行。

然后你考虑换一个方法,从网格图入手。
然后你会发现因为是一行一列,那我们可以把每行每列由墙分开的块都找出来。
那这些块每个至多放一个点,毕竟放了别的都不能放。

那你会发现行之间,列之间没有关系,所以它就变成了一个二分图!
那你考虑这些行列之间会怎样。
你可以考虑用一个边代表一个空地放一个点:这个点要放你就要连这个边,让这两个块都不能再放了。
然后跑最大匹配就是答案了。

代码

#include<queue>
#include<cstdio>
#include<iostream>
#define INF 0x3f3f3f3f3f3f3f3f

using namespace std;

const int N = 50 + 5;
const int M = N * N + 100;
int n, m, npl[M << 1], nl[M << 1], nr[M << 1];
struct node {
	int x, to, nxt, op;
}e[(M * M) << 1];
int le[M << 1], KK, S, T, tot;
int lee[M << 1], deg[M << 1];
char a[N][N];

//网络流模板 
void add(int x, int y, int z) {
	e[++KK] = (node){z, y, le[x], KK + 1}; le[x] = KK;
	e[++KK] = (node){0, x, le[y], KK - 1}; le[y] = KK;
}

bool bfs() {
	for (int i = 1; i <= tot; i++) lee[i] = le[i], deg[i] = 0;
	queue <int> q; q.push(S); deg[S] = 1;
	while (!q.empty()) {
		int now = q.front(); q.pop();
		for (int i = le[now]; i; i = e[i].nxt)
			if (e[i].x && !deg[e[i].to]) {
				deg[e[i].to] = deg[now] + 1;
				if (e[i].to == T) return 1; q.push(e[i].to);
			}
	}
	return 0;
}

int dfs(int now, int sum) {
	if (now == T) return sum;
	int go = 0;
	for (int &i = lee[now]; i; i = e[i].nxt)
		if (e[i].x && deg[e[i].to] == deg[now] + 1) {
			int this_go = dfs(e[i].to, min(sum - go, e[i].x));
			if (this_go) {
				e[i].x -= this_go; e[e[i].op].x += this_go;
				go += this_go; if (go == sum) return go;
			}
		}
	if (go != sum) deg[now] = -1; return go;
}

int dinic() {
	int re = 0;
	while (bfs()) re += dfs(S, INF);
	return re;
}

int main() {
	int TT; scanf("%d", &TT);
	for (int qq = 1; qq <= TT; qq++) {
		scanf("%d %d", &n, &m);//读入 
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++) {
				a[i][j] = getchar(); while (a[i][j] != '#' && a[i][j] != '*' && a[i][j] != 'o') a[i][j] = getchar();
			}
		
		tot = 0; int nn = 0, mm = 0;//先看列的 
		for (int i = 1; i <= n; i++) {
			for (int L = 1, R; L <= m; L = R + 1) {
				R = L; if (a[i][L] == '#') continue;
				bool kong = (a[i][L] == 'o');
				while (R < m && a[i][R + 1] != '#') R++, kong |= (a[i][R] == 'o');//找没有墙的一段 
				if (kong) {//要有空地 
					nn++; tot++; npl[tot] = i; nl[tot] = L; nr[tot] = R;
				}
			}
		}
		for (int i = 1; i <= m; i++) {//行的 
			for (int L = 1, R; L <= n; L = R + 1) {
				R = L; if (a[L][i] == '#') continue;
				bool kong = (a[L][i] == 'o');
				while (R < n && a[R + 1][i] != '#') R++, kong |= (a[R][i] == 'o');//跟上面同理 
				if (kong) {
					mm++; tot++; npl[tot] = i; nl[tot] = L; nr[tot] = R;
				}
			}
		}
		
		//然后枚举行和列找有交的连边
		for (int i = 1; i <= nn; i++)
			for (int j = 1; j <= mm; j++) {
				if (nl[i] <= npl[nn + j] && npl[nn + j] <= nr[i])
					if (nl[nn + j] <= npl[i] && npl[i] <= nr[nn + j])//判断是否有交
						if (a[npl[i]][npl[nn + j]] == 'o')//判断交点是否是空地 
							add(i, nn + j, 1); 
			}
		
		S = ++tot; T = ++tot;
		for (int i = 1; i <= nn; i++) add(S, i, 1);
		for (int i = 1; i <= mm; i++) add(nn + i, T, 1);
		
		printf("Case :%d\n%d\n", qq, dinic());
		
		for (int i = 1; i <= tot; i++) le[i] = 0; KK = 0;//清空 
	}
	
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值