BZOJ1565——[NOI2009]植物大战僵尸
1、题意:有一些点,点与点之间有保护关系,每个点都有一个权值,求能获得的最大值
2、分析:裸的最大权闭合图,用网络流进行求解,然后我们发现点与点之间的保护关系可能构成环,这样网络流是无法处理的,然后我们拓扑排序去掉那些不能获得的点。注意!!!!这里的环是不能用强连通来强行去掉的,因为——比如有一个点,他两端与它相连的点在环内,那么这个点你也去不掉
最大权闭合图模型:建立源点s和汇点t,将所有正权点连向s,容量为点权,将所有负权点连向t,容量为点权的相反数,原图中的边容量全部设成inf,跑一边最小割,用所有正权点的总和减一下就是答案了
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 500010
#define inf 214748364
inline int read(){
char ch = getchar(); int x = 0, f = 1;
while(ch < '0' || ch > '9'){
if(ch == '-') f = -1;
ch = getchar();
}
while('0' <= ch && ch <= '9'){
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
namespace dinic{
struct Edge{
int from, to, cap, flow, next;
} G[M];
int head[M], cur[M], tot;
int vis[M], d[M];
int s, t;
inline void init(){
tot = -1;
memset(head, -1, sizeof(head));
}
inline void add(int u, int v, int w){
G[++ tot] = (Edge){u, v, w, 0, head[u]};
head[u] = tot;
G[++ tot] = (Edge){v, u, 0, 0, head[v]};
head[v] = tot;
}
inline bool BFS(){
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
vis[s] = 1;
d[s] = 0;
while(!Q.empty()){
int x = Q.front(); Q.pop();
for(int i = head[x]; i != -1; i = G[i].next){
Edge& e = G[i];
if(e.cap > e.flow && !vis[e.to]){
vis[e.to] = 1;
d[e.to] = d[x] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}
inline int DFS(int x, int a){
if(x == t || a == 0) return a;
int flow = 0, f;
for(int &i = cur[x]; i != -1; i = G[i].next){
Edge& e = G[i];
if(d[e.to] == d[x] + 1 && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0){
e.flow += f;
G[i ^ 1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}
inline int ans(){
int flow = 0;
while(BFS()){
for(int i = s; i <= t; i ++) cur[i] = head[i];
flow += DFS(s, inf);
}
return flow;
}
}
int val[M];
struct Edge{
int u, v, next;
} G[M];
int head[M], tot;
int du[M], ok[M];
int n, m;
inline void addedge(int u, int v){
// printf("%d %d\n", u, v);
G[++ tot] = (Edge){u, v, head[u]};
head[u] = tot;
du[v] ++;
}
inline void toposort(){
queue<int> Q;
for(int i = 1; i <= n * m; i ++) if(!du[i]){
Q.push(i);
ok[i] = 1;
}
while(!Q.empty()){
int x = Q.front(); Q.pop();
for(int i = head[x]; i != -1; i = G[i].next){
du[G[i].v] --;
if(!du[G[i].v]){
Q.push(G[i].v);
ok[G[i].v] = 1;
}
/*puts("fuck");
printf("%d\n", i);*/
}
}
}
int main(){
n = read(), m = read();
#define Num(i, j) (i - 1) * m + j
memset(head, -1, sizeof(head));
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
int num = Num(i, j);
val[num] = read(); int op = read();
for(int k = 1; k <= op; k ++){
int x = read(), y = read();
x ++; y ++;
addedge(num, Num(x, y));
}
if(j != m) addedge(num + 1, num);
}
}
toposort();
//puts("fuck");
dinic::s = 0; dinic::t = n * m + 1;
dinic::init();
int res = 0;
for(int i = 1; i <= n * m; i ++) if(ok[i]){
if(val[i] > 0) res += val[i], dinic::add(dinic::s, i, val[i]);
else dinic::add(i, dinic::t, -val[i]);
for(int j = head[i]; j != -1; j = G[j].next){
if(ok[G[j].v]){
dinic::add(G[j].v, i, inf);
}
}
}
printf("%d\n", res - dinic::ans());
return 0;
}
BZOJ1565——[NOI2009]植物大战僵尸的更多相关文章
- 【最大权闭合子图 tarjan】bzoj1565: [NOI2009]植物大战僵尸
dinic+tarjan板子练手题 Description Plants vs. Zombies(PVZ)是最近十分风靡的一款小游戏.Plants(植物)和Zombies(僵尸)是游戏的主角,其 中P ...
- BZOJ1565: [NOI2009]植物大战僵尸
Description Input Output 仅包含一个整数,表示可以获得的最大能源收入.注意,你也可以选择不进行任何攻击,这样能源收入为0. Sample Input 3 2 10 0 20 0 ...
- 【bzoj1565】 NOI2009—植物大战僵尸
http://www.lydsy.com/JudgeOnline/problem.php?id=1565 (题目链接) 题意 给出$n*m$的棋盘,僵尸攻击每个格子可以获得$v$的分数,每个格子又会保 ...
- 【bzoj1565】[NOI2009]植物大战僵尸
1565: [NOI2009]植物大战僵尸 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2164 Solved: 1001[Submit][Stat ...
- 图论(网络流):COGS 410. [NOI2009] 植物大战僵尸
410. [NOI2009] 植物大战僵尸 ★★★ 输入文件:pvz.in 输出文件:pvz.out 简单对比时间限制:2 s 内存限制:512 MB [问题描述] Plants vs ...
- P2805 [NOI2009]植物大战僵尸
题目地址:P2805 [NOI2009]植物大战僵尸 最大权闭合子图 若有向图 \(G\) 的子图 \(V\) 满足: \(V\) 中顶点的所有出边均指向 \(V\) 内部的顶点,则称 \(V\) 是 ...
- COGS410. [NOI2009] 植物大战僵尸
410. [NOI2009] 植物大战僵尸 ★★★ 输入文件:pvz.in 输出文件:pvz.out 简单对比时间限制:2 s 内存限制:512 MB [问题描述] Plants vs ...
- BZOJ 1565: [NOI2009]植物大战僵尸
1565: [NOI2009]植物大战僵尸 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2317 Solved: 1071[Submit][Stat ...
- 【刷题】BZOJ 1565 [NOI2009]植物大战僵尸
Description Plants vs. Zombies(PVZ)是最近十分风靡的一款小游戏.Plants(植物)和Zombies(僵尸)是游戏的主角,其中Plants防守,而Zombies进攻. ...
随机推荐
- Eclipse InstaSearch搜索词法 (很多并不支持)
1. 中文翻译 http://www.cnblogs.com/xing901022/p/4974977.html 2. 英文原文 http://lucene.apache.org/core/3_0_3 ...
- mysql 有两种数据库引擎发音
mysql 有两种数据库引擎 一种是 MyISAM,一种是 InnoDB MyISAM 发音为 "my-z[ei]m"; InnoDB 发音为 "in-no-db&quo ...
- C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 忘记密码功能改进、手机短信、电子邮件
由于我们的系统接近有100000个用户账户,经常会有忘记密码的时候,用户多了,很小的一个功能,每天都会有很多人在用,每个功能都非常友善,会提高提系统的效率,提高用户体验. 一天最多能返回3次手机短信, ...
- c# 调用c++DLL方法及注意事项
引用命名空间 using System.Runtime.InteropServices 调用方法: 一.静态加载 用DllImprot方式来加载c++DLL.如下格式: //对应c++方法 //voi ...
- redis主从同步
本文是在window环境下的主从同步 1.redis是如何实现主从同步的 redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从) ...
- sublime text3下BracketHighlighter的配置方法
st3的配置方法和st2是有区别的,所以网上搜索到的方法大多不能用,我google之后总结了一下. 一. 1.在st3中按preferences-->package settings--> ...
- PyQt 自定义信号带参数
import sys from PyQt5.QtCore import pyqtSignal, QObject from PyQt5.QtWidgets import QMainWindow, QAp ...
- awk 用法(使用入门)
转自:http://www.cnblogs.com/emanlee/p/3327576.html awk 用法:awk ' pattern {action} ' 变量名 含义 ARGC 命 ...
- 解决:error: Cannot fetch repo (TypeError: expected string or buffer)
同步源码,问题重现: Fetching project platform/external/libopus Fetching project repo error: Cannot fetch repo ...
- 怎么样修改小猪cms(从功能库添加)模块关键字
需求:修改或者添加从功能库添加中的关键字 这里以添加咨询投诉为列: 找到wwwroot\PigCms\Lib\Action\User目录下的LinkAction.class.php文件(手动找不到直接 ...