[SCOI2007]蜥蜴 BZOJ1066 最大流

题目背景

07四川省选

题目描述

在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外。

每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平面距离不超过d的任何一个石柱上。石柱都不稳定,每次当蜥蜴跳跃时,所离开的石柱高度减1(如果仍然落在地图内部,则到达的石柱高度不变),如果该石柱原来高度为1,则蜥蜴离开后消失。以后其他蜥蜴不能落脚。任何时刻不能有两只蜥蜴在同一个石柱上。

输入输出格式

输入格式:

输入第一行为三个整数r,c,d,即地图的规模与最大跳跃距离。以下r行为石柱的初始状态,0表示没有石柱,1~3表示石柱的初始高度。以下r行为蜥蜴位置,“L”表示蜥蜴,“.”表示没有蜥蜴。

输出格式:

输出仅一行,包含一个整数,即无法逃离的蜥蜴总数的最小值。

输入输出样例

输入样例#1: 复制
5 8 2
00000000
02000000
00321100
02000000
00000000
........
........
..LLLL..
........
........
输出样例#1: 复制
1

说明

100%的数据满足:1<=r, c<=20, 1<=d<=4

 

首先将每一个石块拆为两个点,容量为其高度;

设源点和汇点,

st 和每一个蜥蜴的位置连容量为1的边,

如果可以跳出自然与ed连inf 的边;

如果在给定的dis范围内,每个点的出与另一个点的入连inf的边;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
	ll x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/


int n, m;
int st, ed;

struct node {
	int  u, v, nxt, w;
}edge[maxn<<1];

int head[maxn], cnt;
void addedge(int u, int v, int w) {
	edge[cnt].u = u; edge[cnt].v = v; edge[cnt].w = w;
	edge[cnt].nxt = head[u]; head[u] = cnt++;
}
int rk[maxn];

int bfs() {
	queue<int>q;
	ms(rk); rk[st] = 1;
	q.push(st);
	while (!q.empty()) {
		int tmp = q.front(); q.pop();
		for (int i = head[tmp]; i != -1; i = edge[i].nxt) {
			int to = edge[i].v;
			if (rk[to] || edge[i].w <= 0)continue;
			rk[to] = rk[tmp] + 1; q.push(to);
		}
	}
	return rk[ed];
}

int dfs(int u, int flow) {
	if (u == ed)return flow;
	int add = 0;
	for (int i = head[u]; i != -1 && add < flow; i = edge[i].nxt) {
		int v = edge[i].v;
		if (rk[v] != rk[u] + 1 || !edge[i].w)continue;
		int tmpadd = dfs(v, min(edge[i].w, flow - add));
		if (!tmpadd) { rk[v] = -1; continue; }
		edge[i].w -= tmpadd; edge[i ^ 1].w += tmpadd; add += tmpadd;
	}
	return add;
}

int ans;
void dinic() {
	while (bfs())ans += dfs(st, inf);
}

int mp[100][100];
int hight[100][100];
int x[maxn], y[maxn];
int fg[1000][1000];

double dis(int a, int b, int x, int y) {
	return (a - x)*(a - x) + (b - y)*(b - y);
}

double getid(int a, int b) {
	return (a - 1)*m + b;
}
int main()
{
	//ios::sync_with_stdio(0);
	memset(head, -1, sizeof(head));
	rdint(n); rdint(m); int d; rdint(d);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			char ch; cin >> ch;
			hight[i][j] = ch - '0';
		}
	}
	int tot = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			char ch; cin >> ch;
			if (ch == 'L') {
				fg[i][j] = 1; tot++;// 总的蜥蜴数量
			}
		}
	}
	st = 2 * n*m + 1; ed = st + 1;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			int id = getid(i, j);
			if (hight[i][j]) {
				// 拆点,分为出、入
				addedge(id, id + n * m, hight[i][j]);
				addedge(id + n * m, id, 0);
				if (i + d > n || i - d<1 || j + d>m || j - d < 1) {
					// 跳出
					addedge(id + n * m, ed, inf);
					addedge(ed, id + n * m, 0);
				}
				if (fg[i][j]) {
					addedge(st, id, 1); addedge(id, st, 0);
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			for (int a = 1; a <= n; a++) {
				for (int b = 1; b <= m; b++) {
					if (dis(i, j, a, b) <= d * d) {
						int id1 = getid(i, j);
						int id2 = getid(a, b);
						addedge(id1 + n * m, id2, inf);
						addedge(id2, id1 + n * m, 0);
						addedge(id2 + n * m, id1, inf);
						addedge(id1, id2 + n * m, 0);
					}
				}
			}
		}
	}
	dinic();
	cout << tot - ans << endl;
	return 0;
}

 

posted @ 2018-12-20 09:53  NKDEWSM  阅读(145)  评论(0编辑  收藏  举报