NOI2009植物大战僵尸
这题应该分两步来做:
1、拓扑排序,去掉无敌点
2、求最大闭合子图
需要注意几点:
1、拓扑排序时,如果(i,j)可以攻击到(x,y),那么增加(x,y)的入度,而不是(i,j)的入度
因为入度代表着要攻击它需要事先攻击几个点
2、求最大闭合子图时,用所有的正权点-最大流
3、求最大闭合子图时,如果(i,j)可以攻击到(x,y),那么连一条边(x,y)到(i,j),容量为正无穷
因为在最大闭合子图中边(x,y)到(i,j)意味着选(x,y)就必须要选(i,j),这与实际含义相符
4、s到正权点,容量为正权点的点权
负权点到t,容量为负权点的点权的绝对值
5、要做好对数据规模的估计
代码:
type node=record
go,next,c:longint;
end;
var e:array[..] of node;
head,tail,i,n,m,j,tot,max,ans,s,t,x,y:longint;
w,cur,first,inp,q,h:array[..] of longint;
can:array[..] of boolean;
a:array[..,..] of longint;
procedure insert(x,y,z:longint);
begin
inc(tot);
e[tot].go:=y;
e[tot].c:=z;
e[tot].next:=first[x];
first[x]:=tot;
inc(tot);
e[tot].go:=x;
e[tot].c:=;
e[tot].next:=first[y];
first[y]:=tot;
end;
function min(x,y:longint):longint;
begin
if x<y then exit(x) else exit(y);
end;
procedure init;
begin
readln(n,m);
fillchar(inp,sizeof(inp),);
for i:= to n*m do
begin
read(w[i]);read(a[i,]);
for j:= to a[i,] do
begin
read(x,y);inc(x);inc(y);
a[i,j]:=(x-)*m+y;
inc(inp[(x-)*m+y]);
end;
if i mod m<> then
begin
inc(a[i,]);a[i,a[i,]]:=i-;inc(inp[i-]);
end;
readln;
end;
end;
procedure topsort;
begin
head:=;tail:=;
fillchar(q,sizeof(q),);
fillchar(can,sizeof(can),false);
for i:= to n*m do
if inp[i]= then
begin
can[i]:=true;inc(tail);q[tail]:=i;
end;
while head<tail do
begin
inc(head);
x:=q[head];
for i:= to a[x,] do
begin
y:=a[x,i];
dec(inp[y]);
if inp[y]= then
begin
can[y]:=true;
inc(tail);
q[tail]:=y;
end;
end;
end;
end;
procedure makegraph;
begin
max:=;
for i:= to n*m do
if can[i] then
begin
if w[i]> then inc(max,w[i]);
for j:= to a[i,] do
begin
y:=a[i,j];
if can[y] then insert(y,i,maxlongint>>);
end;
if w[i]> then insert(s,i,w[i])
else insert(i,t,-w[i]);
end;
end;
function bfs:boolean;
var i,x,y:longint;
begin
fillchar(h,sizeof(h),);
fillchar(q,sizeof(q),);
head:=;tail:=;q[]:=s;h[s]:=;
while head<tail do
begin
inc(head);
x:=q[head];
i:=first[x];
while i<> do
begin
y:=e[i].go;
if (h[y]=) and (e[i].c<>) then
begin
h[y]:=h[x]+;
inc(tail);
q[tail]:=y;
end;
i:=e[i].next;
end;
end;
exit(h[t]<>);
end;
function dfs(x,f:longint):longint;
var i,y,tmp,used:longint;
begin
if (x=t) or (f=) then exit(f);
i:=cur[x];tmp:=;used:=;
while i<> do
begin
y:=e[i].go;
if (h[y]=h[x]+) and (e[i].c<>) then
begin
tmp:=dfs(y,min(e[i].c,f-used));
dec(e[i].c,tmp);
inc(e[i xor ].c,tmp);
if e[i].c<> then cur[x]:=i;
inc(used,tmp);
if used=f then exit(f);
end;
i:=e[i].next;
end;
if used= then h[x]:=-;
exit(used);
end; procedure dinic;
begin
while bfs do
begin
for i:= to n*m+ do cur[i]:=first[i];
inc(ans,dfs(s,maxlongint>>));
end;
end; procedure main;
begin
tot:=;
s:=;t:=n*m+;
topsort;
makegraph;
dinic;
writeln(max-ans);
end;
begin
init;
main;
end.
NOI2009植物大战僵尸的更多相关文章
- 图论(网络流):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进攻. ...
- 【bzoj1565】[NOI2009]植物大战僵尸
1565: [NOI2009]植物大战僵尸 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2164 Solved: 1001[Submit][Stat ...
- 【最大权闭合子图 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$的分数,每个格子又会保 ...
- luogu2805 [NOI2009]植物大战僵尸
想象一下,要搞掉一个植物,必须先搞掉另一些植物--我们可以发现这是一个最大权闭合子图的问题. 最大权闭合子图的话,太空飞行计划问题是一个入门题,可以一看. 然而我们手玩一下样例就会惊恐地发现,保护关系 ...
随机推荐
- (转载)Cocos2dx-OpenGL ES2.0教程:初识MVP(3)
在上一篇文章中,我在介绍vertex shader的时候挖了一个坑:CC_MVPMatrix.它其实是一个uniform,每一个cocos2d-x预定义的shader都包含有这个uniform, 但是 ...
- K-meams文本聚类算法C++实现
FROM:http://www.cnblogs.com/finallyliuyu/archive/2010/09/03/1817348.html 头文件: #ifndef _Preprocess_H ...
- bnuoj 4187 GCC (数论)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=4187 [题意]:如题 [题解]:取n,m的最小值进行遍历就可以了: 注意 0 1 这组测试数据 [c ...
- 手写归并排序(MergeSort)
#include<iostream> #include<stdio.h> #include<algorithm> #define N 10000 using nam ...
- 然爸读书笔记(2014-10)----Bootsrap用户手册
本来其实这方面的知识,可以直接在网站上看,但是自己是web前端的小白.基本不懂.所以,还是买一本书来看,更好些.体现出我对前端的重视. Bootstrap是快速开发Web应用程序的前端工具包.它是一个 ...
- 2733: [HNOI2012]永无乡 - BZOJ
Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以 ...
- 关于ref与out的区别
写在最前面 这几天一直在公司接受培训,都是一些基础的知识,同时也乘着这个机会巩固一下自己的基础,基础太重要了.前些时一直看的是多线程方面的知识,接下来我会写一些其他方面的知识,毕竟作为一个实习新人得和 ...
- 电商安全无小事,如何有效地抵御 CSRF 攻击?
现在,我们绝大多数人都会在网上购物买东西.但是很多人都不清楚的是,很多电商网站会存在安全漏洞.比如乌云就通报过,国内很多家公司的网站都存在 CSRF 漏洞.如果某个网站存在这种安全漏洞的话,那么我们在 ...
- Stretch a row if data overflows in jasper reports
My band stretches if necessary with the following conditions, I do not know yours. I have started wi ...
- tomcat部署javaweb项目的三种方式
一.将项目文件夹或war包直接拷贝到tomcat的webapps下 二.在Tomcat\conf\Catalina\localhost下建立xml文件 修改内容如下<Context path=& ...