首页 > 代码库 > BZOJ 1066 SCOI 2007 蜥蜴 最大流

BZOJ 1066 SCOI 2007 蜥蜴 最大流

题目大意:给出一张图,每一个点有一个寿命,当有这个寿命值个蜥蜴经过后这个点就会消失,一个蜥蜴可以跳到距离不超过d的点上,问最少有多少只蜥蜴无法跳出这张图。


思路:我们将每个点拆点,然后限制流量为这个点的寿命,之后源点向每个蜥蜴连边,互相能够到达的点之间连边,能够跳出这个图的点和汇点连边,跑最大流就是这个图中最多能够跑出去的蜥蜴数量,最后在用总数减去就是最少不能逃出去的数量。


CODE:

#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 50
#define MAXP 1010
#define MAXE 5000010
#define INF 0x3f3f3f3f
#define S 0
#define T ((m * n << 1) + 2)
using namespace std;

int m,n,d,Ts;
int src[MAX][MAX],num[MAX][MAX],cnt;
char s[MAX];

int head[MAXP],total = 1;
int next[MAXE],aim[MAXE],flow[MAXE];

int deep[MAXP];

inline void Add(int x,int y,int f)
{
	next[++total] = head[x];
	aim[total] = y;
	flow[total] = f;
	head[x] = total;
}

inline void Insert(int x,int y,int f)
{
	Add(x,y,f);
	Add(y,x,0);
}

inline double Calc(int x1,int y1,int x2,int y2)
{
	return sqrt((double)(x1 - x2) * (x1 - x2) + (double)(y1 - y2) * (y1 - y2));
}

inline bool BFS()
{
	static queue<int> q;
	while(!q.empty())	q.pop();
	memset(deep,0,sizeof(deep));
	deep[S] = 1;
	q.push(S);
	while(!q.empty()) {
		int x = q.front(); q.pop();
		for(int i = head[x]; i; i = next[i])
			if(flow[i] && !deep[aim[i]]) {
				deep[aim[i]] = deep[x] + 1;
				q.push(aim[i]);
				if(aim[i] == T)	return true;
			}
	}
	return false;
}

int Dinic(int x,int f)
{
	if(x == T)	return f;
	int temp = f;
	for(int i = head[x]; i; i = next[i])
		if(deep[aim[i]] == deep[x] + 1 && flow[i] && temp) {
			int away = Dinic(aim[i],min(flow[i],temp));
			if(!away)	deep[aim[i]] = 0;
			flow[i] -= away;
			flow[i^1] += away;
			temp -= away;
		}
	return f - temp;
}

int main()
{
	cin >> m >> n >> d;
	for(int i = 1; i <= m; ++i)
		for(int j = 1; j <= n; ++j) {
			scanf("%1d",&src[i][j]),num[i][j] = ++cnt;
			if(src[i][j])
				Insert(num[i][j] << 1,num[i][j] << 1|1,src[i][j]);
		}
	for(int i = 1; i <= m; ++i) {
		scanf("%s",s + 1);
		for(int j = 1; j <= n; ++j)
			if(s[j] == 'L')
				Insert(S,num[i][j] << 1,1),++Ts;
	}
	for(int i = 1; i <= m; ++i)
		for(int j = 1; j <= n; ++j)
			for(int _i = 1; _i <= m; ++_i)
				for(int _j = 1; _j <= n; ++_j) {
					if(i == _i && j == _j)	continue;
					if(Calc(i,j,_i,_j) > d)	continue;
					Insert(num[i][j] << 1|1,num[_i][_j] << 1,INF);
				}
	for(int i = 1; i <= m; ++i)
		for(int j = 1; j <= n; ++j)
			if(i <= d || j <= d || m - i < d || n - j < d)
				Insert(num[i][j] << 1|1,T,INF);	
	int max_flow = 0;
	while(BFS())
		max_flow += Dinic(S,INF);
	cout << Ts - max_flow << endl;
	return 0;	
}


BZOJ 1066 SCOI 2007 蜥蜴 最大流