P2805 [NOI2009]植物大战僵尸 + 最大权闭合子图 X 拓扑排序
传送门:https://www.luogu.org/problemnew/show/P2805
题意
有一个n * m的地图,你可以操纵僵尸从地图的右边向左边走,走的一些地方是有能量值的,有些地方会被一些植物保护起来不能走,只有先吃掉特定植物才能走一些地方。求最大可能拿到的能量值和
思路
最大权闭合子图,由于僵尸只能从一行的右边一步一步走到左边,所以每个格子向右边连一条inf的边(表示选了这个点,右边这个点必选),然后有保护的原因,从被保护的格子向保护的格子连一条inf的边。然后就是最大权闭合子图的操作,源点向正权值格子连容量为这个权值的边,负权值的格子向终点连容量为这个权值绝对值的边。
由于图中有环的存在,我们要把环上以及环之后的点都抹去。
然后跑一遍dinic(),算出最小割,用总的正权值 - 这个最小割就是答案。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
\\ Λ_Λ 来了老弟
\('ㅅ')
> ⌒ヽ
/ へ\
/ / \\
レ ノ ヽ_つ
/ /
/ /|
( (ヽ
| |、\
| 丿 \ ⌒)
| | ) /
'ノ ) Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = ;
int val[maxn],vis[maxn],du[maxn];
vector<int>mp[maxn];
vector<pii>ptt[maxn];//受保护的点。
int n,m;
void topo(){
queue<int>que;
for(int i=; i<=n*m; i++){
if(du[i] == ) que.push(i);
}
while(!que.empty()){
int u = que.front(); que.pop();
vis[u] = ;
for(int i=; i<mp[u].size(); i++){
int v = mp[u][i];
du[v] --;
if(du[v] == ) que.push(v);
}
}
} struct E{
int v,w;
int nxt;
}edge[];
int gtot = , head[maxn];
void addedge(int u,int v,int w){
edge[gtot].v = v;
edge[gtot].w = w;
edge[gtot].nxt = head[u];
head[u] = gtot++; edge[gtot].v = u;
edge[gtot].w = ;
edge[gtot].nxt = head[v];
head[v] = gtot++;
} int dis[maxn],cur[maxn];
bool bfs(int s,int t){
memset(dis, inf, sizeof(dis));
dis[s] = ;
queue<int>que; que.push(s); for(int i=s; i<=t; i++) cur[i] = head[i];
while(!que.empty()){
int u = que.front(); que.pop();
for(int i=head[u]; ~i; i = edge[i].nxt){
int v = edge[i].v, w = edge[i].w;
if(w > && dis[v] > dis[u] + ){
dis[v] = dis[u] + ;
que.push(v);
}
}
}
return dis[t] < inf;
} int dfs(int u,int t,int maxflow){
if(u == t || maxflow == ) return maxflow;
for(int i=cur[u]; ~i; i = edge[i].nxt){
cur[u] = i;
int v = edge[i].v, w = edge[i].w;
if(w > && dis[v] == dis[u] + ) {
int f = dfs(v, t, min(w, maxflow));
if(f > ) {
edge[i].w -= f;
edge[i^].w += f;
return f;
}
}
}
return ;
}
int dinic(int s,int t){
int flow = ;
while(bfs(s, t)){
while(int f = dfs(s,t,inf)) flow += f;
}
return flow;
}
int main(){
memset(head, -, sizeof(head));
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++){
for(int j=; j<=m; j++){
int u = (i-)*m + j, v = u + ;
scanf("%d", &val[u]);
if(j < m){
du[u]++;mp[v].pb(u);//v -> u
}
int q; scanf("%d", &q);
while(q -- ){
int x,y;
scanf("%d%d", &x, &y);
x++,y++;
int p = (x-)*m+y;
du[p]++; mp[u].pb(p);
ptt[u].pb(pii(x,y));
}
}
} int s = , t = n*m+; topo(); int sum = ;
for(int i=; i<=n; i++){
for(int j=; j<=m; j++){ int u = (i-)*m + j, v = u + ;
if(vis[u] == ) continue;
if(j < m && vis[v])addedge(u, v, inf); if(val[u] >= ) addedge(s, u, val[u]), sum += val[u];
else addedge(u, t, -*val[u]); for(int k=; k < ptt[u].size(); k++){
int x = ptt[u][k].fi, y = ptt[u][k].se;
int p = (x - ) * m + y;
addedge(p, u, inf);
}
}
} printf("%d\n", sum - dinic(s,t));
return ;
}
P2805 [NOI2009]植物大战僵尸 + 最大权闭合子图 X 拓扑排序的更多相关文章
- BZOJ 1565 / P2805 [NOI2009]植物大战僵尸 (最大权闭合子图 最小割)
题意 自己看吧 BZOJ传送门 分析 - 这道题其实就是一些点,存在一些二元限制条件,即如果要选uuu则必须选vvv.求得到的权值最大是多少. 建一个图,如果选uuu必须选vvv,则uuu向vvv连边 ...
- bzoj1565: [NOI2009]植物大战僵尸 最大权闭合子图,tarjan
bzoj1565: [NOI2009]植物大战僵尸 链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1565 思路 很容易的想到最大权闭合子图 ...
- BZOJ1565[NOI2009]植物大战僵尸——最大权闭合子图+拓扑排序
题目描述 Plants vs. Zombies(PVZ)是最近十分风靡的一款小游戏.Plants(植物)和Zombies(僵尸)是游戏的主角,其中Plants防守,而Zombies进攻.该款游戏包含多 ...
- Bzoj 1565: [NOI2009]植物大战僵尸 最大权闭合图,拓扑排序
题目: http://cojs.tk/cogs/problem/problem.php?pid=410 410. [NOI2009] 植物大战僵尸 ★★★ 输入文件:pvz.in 输出文件:p ...
- BZOJ 1565 植物大战僵尸 最大权闭合子图+网络流
题意: 植物大战僵尸,一个n*m的格子,每 个格子里有一个植物,每个植物有两个属性: (1)价值: (2)保护集合,也就是这个植物可以保护矩阵中的某些格子. 现在你是僵尸,你每次只能从(i,m) 格子 ...
- 洛谷 P2805 [NOI2009]植物大战僵尸 解题报告
P2805 [NOI2009] 植物大战僵尸 题目描述 Plants vs. Zombies(PVZ)是最近十分风靡的一款小游戏.Plants(植物)和Zombies(僵尸)是游戏的主角,其中Plan ...
- BZOJ 1565 Luogu P2805 [NOI2009]植物大战僵尸 (Tarjan判环、最小割)
我: "立个flag 14点之前调完这题" 洛谷AC时间: 2019-06-24 14:00:16 实力打脸... 网络流板子从来写不对系列 题目链接: (BZOJ) https: ...
- P2805 [NOI2009]植物大战僵尸(最小割+拓扑排序)
题意: n*m的矩阵,每个位置都有一个植物.每个植物都有一个价值(可以为负),以及一些它可以攻击的位置.从每行的最右面开始放置僵尸,僵尸从右往左行动,当僵尸在植物攻击范围内时会立刻死亡.僵尸每到一个位 ...
- 洛谷$P2805\ [NOI2009]$植物大战僵尸 网络流
正解:网络流 解题报告: 传送门$QwQ$ 题面好长昂,,,我大概概括下$QwQ$?有个$n\cdot m$的网格,每个格子有一株植物,击溃一株植物$(x,y)$需要付出$S_{(x,y)}$的代价( ...
随机推荐
- android 界面提示框架WisdomProgressHUD,为金典而生
一:简述 今天给android开发者们,推荐一个金典的界面提示框架WisdomProgressHUD,使用简洁方便. WisdomProgressHUD 是一个半透明的 HUD 指示器. Wisdom ...
- pheatmap绘制“热图”,你需要的都在这
热图可以聚合大量的数据,并可以用一种渐进色来优雅地表现,可以很直观地展现数据的疏密程度或频率高低. 本文利用R语言 pheatmap 包从头开始绘制各种漂亮的热图.参数像积木,拼凑出你最喜欢的热图即可 ...
- markdown常用方法
Markdown格式的普及流行要归功于Github和StackOverflow的流行,随着它们越来越流行,它们支持的Markdown格式也越来越流行. 1.优点 1.Markdown通过内容和样式相分 ...
- 【iOS】tableView:viewForHeaderInSection: 方法未调用
今天遇到这个问题,即重写的方法 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)sec ...
- 【eclipse】No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A
用 eclipse 写 Java 代码时出现了这个问题,详细如下: No enclosing instance of type TestParsingLinkedList is accessible. ...
- ubuntu/deepin 下下载wxpython
1 输入apt-cache search wxpython 如果有返回信息 则输入 sudo apt-get install python-tools 2 否则 1.添加软件源地址到apt列表中.输入 ...
- Java 8原生API也可以开发响应式代码?
前段时间工作上比较忙,这篇文章一直没来得及写,本文是阅读<Java8实战>的时候,了解到Java 8里已经提供了一个异步非阻塞的接口(CompletableFuture),可以实现简单的响 ...
- java虚拟机学习笔记(五)---运行时的数据区域
Java虚拟机所管理的内存包括以下几个运行时的数据区域:方法区,堆,虚拟机栈,本地方法栈,程序计数器.下面对其进行介绍: 程序计数器 它是一块较小的内存空间,可以看做当前线程做执行的字节码的信号指示器 ...
- 使用webstorm调试node.js
折腾半天,还是webstorm顺手,但也遇到一些小问题. 1.代码补全问题 nodeJS自身的补全 File->Project Setting->JavaScript->Librar ...
- HelloDjango 第 08 篇:开发博客文章详情页
作者:HelloGitHub-追梦人物 文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 首页展示的是所有文章的列表,当用户看到感兴趣的文章时,他点击文章的标题或者继续阅读的按 ...