BZOJ 1822 JSOI 2010 Frozen Nova 冷冻波 二分+网络流

题目大意:在平面中有一些巫妖和一些小精灵,还有一些树会阻挡巫妖的视线。每一个巫妖都有一个攻击半径,如果一个小精灵在巫妖的攻击半径内,两点之间的连线没有树木阻挡,那么巫妖就可以秒杀小精灵。每个巫妖都有技能的CD。问最快多长时间可以使小精灵全灭。


思路:看出范围知算法系列。很明显的二分+最大流。二分最短时间,在这个时间内,每个巫妖可以发招time / CD + 1次。那么每次建图就从S到每个巫妖连能够输出的次数流量的边。每个巫妖向能攻击到的小精灵连边,之后每个小精灵向T连边。每次判断max_flow是否等于小精灵数。


CODE:

#include <queue>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 510
#define MAXE 1000000
#define INF 0x3f3f3f3f
#define S 0
#define T (MAX - 1)
using namespace std;
#define min(a,b) ((a) < (b) ? (a):(b))

struct Point{
	int x,y;
}pos[MAX];
struct Complex{
	Point p;
	int r;
	
	void Read() {
		scanf("%d%d%d",&p.x,&p.y,&r);
	}
}src[MAX],tree[MAX];
vector<int> can[MAX];
int _time[MAX];

int cnt,_cnt,trees;

inline double Calc(const Point &p1,const Point &p2)
{
	return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}

inline double GetArea(const Point &p1,const Point &p2,const Point &p3)
{
	double l1 = Calc(p1,p2),l2 = Calc(p2,p3),l3 = Calc(p3,p1);
	double p = (l1 + l2 + l3) / 2;
	return sqrt(p * (p - l1) * (p - l2) * (p - l3));
}

inline bool InRange(const Complex &tree,const Point &p1,const Point &p2)
{
	double area = GetArea(p1,p2,tree.p) * 2;
	double dis = area / Calc(p1,p2);
	return dis <= tree.r;
}

int head[MAX],total;
int next[MAXE],aim[MAXE],flow[MAXE];

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 void BuildGraph(int ans)
{
	memset(head,0,sizeof(head));
	total = 1;
	for(int i = 1; i <= cnt; ++i) {
		Insert(S,i,ans / _time[i] + 1);
		for(vector<int>::iterator it = can[i].begin(); it != can[i].end(); ++it)
			Insert(i,cnt + *it,1);
	}
	for(int i = 1; i <= _cnt; ++i)
		Insert(cnt + i,T,1);
}

int deep[MAX];

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(!deep[aim[i]] && flow[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(temp && deep[aim[i]] == deep[x] + 1 && flow[i]) {
			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 >> cnt >> _cnt >> trees;
	for(int i = 1; i <= cnt; ++i) {
		src[i].Read();
		scanf("%d",&_time[i]);
	}
	for(int i = 1; i <= _cnt; ++i)
		scanf("%d%d",&pos[i].x,&pos[i].y);
	for(int i = 1; i <= trees; ++i)
		tree[i].Read();
	for(int i = 1; i <= cnt; ++i)
		for(int j = 1; j <= _cnt; ++j)
			if(Calc(src[i].p,pos[j]) <= src[i].r) {
				bool flag = true;
				for(int k = 1; k <= trees; ++k)
					if(InRange(tree[k],src[i].p,pos[j]))
						flag = false;
				if(flag)
					can[i].push_back(j);
			}
	int l = 0,r = 10000000,ans = -1;
	while(l <= r) {
		int mid = (l + r) >> 1;
		BuildGraph(mid);
		int max_flow = 0;
		while(BFS())
			max_flow += Dinic(S,INF);
		if(max_flow == _cnt)
			r = mid - 1,ans = mid;
		else	l = mid + 1;
	}
	cout << ans << endl;
	return 0;
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。