[Luogu P2805] [BZOJ 1565] [NOI2009]植物大战僵尸

洛谷传送门
BZOJ传送门

题目描述

Plants vs. Zombies(PVZ)是最近十分风靡的一款小游戏。Plants(植物)和Zombies(僵尸)是游戏的主角,其中Plants防守,而Zombies进攻。该款游戏包含多种不同的挑战系列,比如Protect Your Brain、Bowling等等。其中最为经典的,莫过于玩家通过控制Plants来防守Zombies的进攻,或者相反地由玩家通过控制Zombies对Plants发起进攻。

现在,我们将要考虑的问题是游戏中Zombies对Plants的进攻,请注意,本题中规则与实际游戏有所不同。游戏中有两种角色,Plants和Zombies,每个Plant有一个攻击位置集合,它可以对这些位置进行保护;而Zombie进攻植物的方式是走到植物所在的位置上并将其吃掉。

游戏的地图可以抽象为一个 N N N M M M列的矩阵,行从上到下用 0 0 0 N – 1 N–1 N1编号,列从左到右用 0 0 0 M – 1 M–1 M1编号;在地图的每个位置上都放有一个 P l a n t Plant Plant,为简单起见,我们把位于第 r r r行第 c c c列的植物记为 P r , c P_{r, c} Pr,c

Plants分很多种,有攻击类、防守类和经济类等等。为了简单的描述每个Plant,定义Score和Attack如下:

S c o r e [ P r , c ] Score[P_{r, c}] Score[Pr,c]

Zombie击溃植物 P r , c P_{r, c} Pr,c可获得的能源。若 S c o r e [ P r , c ] Score[P_{r, c}] Score[Pr,c]为非负整数,则表示击溃植物 P r , c P_{r, c} Pr,c可获得能源 S c o r e [ P r , c ] Score[P_{r, c}] Score[Pr,c],若为负数表示击溃 P r , c P_{r, c} Pr,c需要付出能源 − S c o r e [ P r , c ] -Score[P_{r, c}] Score[Pr,c]

A t t a c k [ P r , c ] Attack[P_{r, c}] Attack[Pr,c]

植物 P r , c P_{r, c} Pr,c能够对Zombie进行攻击的位置集合。

Zombies必须从地图的右侧进入,且只能沿着水平方向进行移动。Zombies攻击植物的唯一方式就是走到该植物所在的位置并将植物吃掉。因此Zombies的进攻总是从地图的右侧开始。也就是说,对于第 r r r行的进攻,Zombies必须首先攻击 P r , M − 1 P_{r, M-1} Pr,M1;若需要对 P r , c P_{r, c} Pr,c 0 ≤ c &lt; M − 1 0≤c&lt;M-1 0c<M1)攻击,必须将 P r , M − 1 , P r , M − 2 … P r , c + 1 P_{r,M-1}, P_{r, M-2} … P_{r, c+1} Pr,M1,Pr,M2Pr,c+1先击溃,并移动到位置 ( r , c ) (r, c) (r,c)才可进行攻击。

在本题的设定中,Plants的攻击力是无穷大的,一旦Zombie进入某个Plant的攻击位置,该Zombie会被瞬间消灭,而该Zombie没有时间进行任何攻击操作。因此,即便Zombie进入了一个Plant所在的位置,但该位置属于其他植物的攻击位置集合,则Zombie会被瞬间消灭而所在位置的植物则安然无恙(在我们的设定中,Plant的攻击位置不包含自身所在位置,否则你就不可能击溃它了)。

Zombies的目标是对Plants的阵地发起进攻并获得最大的能源收入。每一次,你可以选择一个可进攻的植物进行攻击。本题的目标为,制定一套Zombies的进攻方案,选择进攻哪些植物以及进攻的顺序,从而获得最大的能源收入。

输入输出格式

输入格式:

输入文件pvz.in的第一行包含两个整数 N , M N, M N,M,分别表示地图的行数和列数。

接下来 N × M N×M N×M行描述每个位置上植物的信息。第 r × M + c + 1 r×M + c + 1 r×M+c+1行按照如下格式给出植物 P r , c P_{r, c} Pr,c的信息:第一个整数为 S c o r e [ P r , c ] Score[P_{r, c}] Score[Pr,c], 第二个整数为集合 A t t a c k [ P r , c ] Attack[P_{r, c}] Attack[Pr,c]中的位置个数 w w w,接下来 w w w个位置信息( r ’ , c ’ r’, c’ r,c),表示 P r , c P_{r, c} Pr,c可以攻击位置第 r ’ r’ r 行第 c ’ c’ c 列。

输出格式:

输出文件pvz.out仅包含一个整数,表示可以获得的最大能源收入。注意,你也可以选择不进行任何攻击,这样能源收入为 0 0 0

输入输出样例

输入样例#1:

复制

3 2
10 0
20 0
-10 0
-5 1 0 0
100 1 2 1
100 0

输出样例#1:

复制

25

说明

约20%的数据满足 1 ≤ N , M ≤ 5 1 ≤ N, M ≤ 5 1N,M5

约40%的数据满足 1 ≤ N , M ≤ 10 1 ≤ N, M ≤ 10 1N,M10

约100%的数据满足 1 ≤ N ≤ 20 , 1 ≤ M ≤ 30 , − 10000 ≤ S c o r e ≤ 10000 1 ≤ N ≤ 20,1 ≤ M ≤ 30,-10000 ≤ Score ≤ 10000 1N201M3010000Score10000

解题分析

最大权闭合子图,先一遍拓扑排序, 求出可以科学吃掉的植物, 然后 S S S向正权植物连边, 负权植物向 T T T连边, 原图中的边都连成 I N F INF INF, 然后跑一遍最小割就好了, 最后答案即为正权和减去最小割。

注意靠右的植物是有保护靠左的植物的作用的。

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <queue>
#include <vector>
#include <algorithm>
#define R register
#define IN inline
#define W while
#define gc getchar()
#define MX 1050
#define get(i, j) (i * m + j + 1)
#define INF 1e8
#define DEBUG printf("%d %d %d\n", from, to, fl)
bool neg;
template <class C>
IN void in(C &x)
{
	x = 0; R char c = gc;
	for (; !isdigit(c); c = gc)
	if (c == '-') neg = true;
	for (;  isdigit(c); c = gc)
	x = (x << 1) + (x << 3) + c - 48;
	if (neg) neg = false, x = -x;
}
template <class C> IN C max(C a, C b) {return a > b ? a : b;}
template <class C> IN C min(C a, C b) {return a < b ? a : b;}
int h[MX], layer[MX], head[MX], val[MX], deg[MX];
std::vector <int> prot[MX];
int cnt, n, m, arr, S, T, sum;
std::queue <int> q;
bool vis[MX];
struct EDGE {int to, nex;} e[200500];
struct Edge {int to, fl, nex;} edge[400500];
IN void add (R int from, R int to, R int fl)
{
	edge[++cnt] = {to, fl, head[from]}, head[from] = cnt;
	edge[++cnt] = {from, 0, head[to]}, head[to] = cnt;
	#ifdef LPA
	DEBUG;
	#endif
}
IN void add(R int from, R int to) {e[++arr] = {to, h[from]}, h[from] = arr;}
namespace Dinic
{
	IN bool BFS()
	{
		std::memset(layer, 0, sizeof(layer));
		layer[S] = 1; R int now; q.push(S);
		W (!q.empty())
		{
			now = q.front(); q.pop();
			for (R int i = head[now]; ~i; i = edge[i].nex)
			{
				if ((!layer[edge[i].to]) && edge[i].fl)
				layer[edge[i].to] = layer[now] + 1, q.push(edge[i].to);
			}
		}
		return layer[T];
	}
	int DFS(R int now, R int avai)
	{
		if (now == T) return avai;
		R int lef = avai, buf;
		for (int &i = head[now]; ~i; i = edge[i].nex)
		{
			if (layer[edge[i].to] == layer[now] + 1 && edge[i].fl)
			{
				buf = DFS(edge[i].to, min(edge[i].fl, lef));
				if (!buf) continue;
				lef -= buf, edge[i].fl -= buf, edge[i ^ 1].fl += buf;
				if (!lef) return avai;
			}
		}
		return avai - lef;
	}
	IN void init()
	{
		int ret = 0; std::memcpy(h, head, sizeof(h));
		W (BFS()) ret += DFS(S, INF), std::memcpy(head, h, sizeof(int) * (T + 1));
		printf("%d", sum - ret);
	}
}
int main(void)
{
	int foo, x, y, id, now;
	in(n), in(m); S = n * m + 1, T = n * m + 2;
	std::memset(head, cnt = -1, sizeof(head));
	for (R int i = 0; i < n; ++i)
	for (R int j = 0; j < m; ++j)
	{
		id = get(i, j);
		in(val[id]), in(foo);
		for (R int i = 1; i <= foo; ++i)
		in(x), in(y), add(id, get(x, y)), prot[get(x, y)].push_back(id), ++deg[get(x, y)];
		if (j) add(id, id - 1), ++deg[id - 1], prot[id - 1].push_back(id);
	}
	for (R int i = 0; i < n; ++i)
	for (R int j = 0; j < m; ++j)
	if (!deg[get(i, j)]) q.push(get(i, j)); 
	W (!q.empty())
	{
		now = q.front(); q.pop();
		vis[now] = true;
		for (R int i = h[now]; i; i = e[i].nex)
		{
			deg[e[i].to]--;
			if (!deg[e[i].to]) q.push(e[i].to);
		}
	}
	for (R int i = 0; i < n; ++i)
	for (R int j = 0; j < m; ++j)
	{
		id = get(i, j);
		if (vis[id])
		{
			if (val[id] > 0) sum += val[id], add(S, id, val[id]);
			else add(id, T, -val[id]);
			for (R int k = prot[id].size() - 1; ~k; --k)
			add(id, prot[id][k], INF);
		}
	}
	Dinic::init();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值