bzoj1066题解
【解题思路】
考虑拆点,把每根石柱拆成两个点,具体可以理解为石柱底部和石柱顶部,能爬到石柱顶部的蜥蜴只有有限只,而且蜥蜴只有爬到了石柱顶部才能跳到其他石柱的底部。
这样,考虑如下建图:
将每个有蜥蜴的石柱底部和源点连边,容量为1;
将每个可以跳出边界的石柱顶部和汇点连边,容量为∞;
将每根石柱顶底相连,容量为该石柱最大承受蜥蜴个数;
将可以相互跳达的任意两根石柱顶底相连,容量为∞。
这样,该图的最大流即为最多可逃脱蜥蜴数,最终答案即为总蜥蜴数-最大流,复杂度o(r3c3d2)。
【参考代码】
#include <bits/stdc++.h>
#define range(i,low,high) for(register int i=(low);i<(high);++i)
#define dange(i,high,low) for(register int i=(high);i>(low);--i) //#define __debug
#ifdef __debug
#define __function__(type) type
#define __procedure__ void
#else
#define __function__(type) __attribute__((optimize("-O2"))) inline type
#define __procedure__ __attribute__((optimize("-O2"))) inline void
#endif using namespace std; static const int N=,M=,INF=0x7f7f7f7f;
static int r,c,d,cardE=; int hed[N+],h[][]; struct edge
{
int fr,to,cap,nxt;
edge(
const int&f=,const int&t=,
const int&c=,const int&x=
):fr(f),to(t),cap(c),nxt(x) {}
}edg[M<<|]; __procedure__ addedge(
const int&fr,const int&to,const int&cp
)
{
edg[cardE]=edge(fr,to,cp,hed[fr]),hed[fr]=cardE++;
}
__procedure__ add_edge(
const int&fr,const int&to,const int&cp
)
{
addedge(fr,to,cp),addedge(to,fr,);
} //SAP {
static const int S=,T=;
int aug[N+],cur[N+],dis[N+],gap[N+],path[N+]; __function__(int) augment()
{
for(int i=T;i!=S;i=edg[path[i]].fr)
{
edg[path[i]].cap-=aug[T],edg[path[i]^].cap+=aug[T];
}
return aug[T];
} __function__(int) SAP(const int&N)
{
memset(aug,,sizeof aug),memset(gap,,sizeof gap);
memset(dis,,sizeof dis),aug[S]=INF,gap[]=N; int ret=;
range(i,,max(N,T)+) cur[i]=hed[i];
for(int fr=S;dis[S]<N;)
{
if(fr==T) fr=S,ret+=augment(); bool flag=;
for(int i=cur[fr];~i;i=edg[i].nxt)
{
int to=edg[i].to;
if(edg[i].cap&&dis[fr]==dis[to]+)
{
aug[to]=min(aug[fr],edg[i].cap),
path[to]=cur[fr]=i,fr=to,flag=; break;
}
}
if(flag)
{
if(!--gap[dis[fr]]) break; dis[fr]=N;
for(int i=hed[fr];~i;i=edg[i].nxt) if(edg[i].cap)
{
dis[fr]=min(dis[fr],dis[edg[i].to]+);
}
++gap[dis[fr]],cur[fr]=hed[fr];
if(fr!=S) fr=edg[path[fr]].fr;
}
}
return ret;
}
//} SAP __function__(bool) ok(const int&x,const int&y)
{
return x+d>=r||x<d||y+d>c||y<=d;
} __function__(int) calc(const int&x,const int&y,const bool&up)
{
return x*c+y+up*(N>>);
} int main()
{
scanf("%d%d%d",&r,&c,&d); int cnt=;
memset(hed,-,sizeof hed);
range(i,,r) range(j,,c+)
{
while(!isdigit(h[i][j]=getchar()));
add_edge(calc(i,j,),calc(i,j,),h[i][j]-='');
}
range(i,,r) range(j,,c+)
{
char ch=getchar();
for(;ch!='.'&&ch!='L';ch=getchar());
if(ch=='L') add_edge(S,calc(i,j,),),++cnt;
}
range(i,,r) range(j,,c+) if(ok(i,j))
{
add_edge(calc(i,j,),T,INF);
}
range(i,,r) range(j,,c+) range(k,-d,d+)
{
int rest=d-abs(k);
range(l,-rest,rest+) if(k||l)
{
int x=i+k,y=j+l;
if(x>=&&x<r&&y>&&y<=c&&h[i][j]&&h[x][y])
{
add_edge(calc(i,j,),calc(x,y,),INF);
}
}
}
return printf("%d\n",cnt-SAP(r*c+<<)),;
}
bzoj1066题解的更多相关文章
- [BZOJ1066][luogu_P2472][SCOI2007]蜥蜴
[BZOJ1066][luogu_P2472][SCOI2007]蜥蜴 试题描述 在一个 \(r\) 行 \(c\) 列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
随机推荐
- php floor()函数 语法
php floor()函数 语法 floor函数是什么意思? php floor()函数用来向下舍入为最接近的整数.语法是floor(number),表示返回不大于参数number的下一个整数,有小数 ...
- thinkphp生成二维码
/** * 生成二维码 * @param string $url url连接 * @param integer $size 尺寸 纯数字 */ function qrcode($url,$size=4 ...
- AcWing 208. 开关问题 (高斯消元+状压)打卡
有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为开就变为关,如果为关就变为开. 你的目 ...
- ASP.NET MVC 分页之HtmlHelper
using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptograph ...
- 求最小生成树(暴力法,prim,prim的堆优化,kruskal)
求最小生成树(暴力法,prim,prim的堆优化,kruskal) 5 71 2 22 5 21 3 41 4 73 4 12 3 13 5 6 我们采用的是dfs的回溯暴力,所以对于如下图,只能搜索 ...
- C语言中#和##的作用
使用#把宏参数变为一个字符串,用##把两个宏参数贴合在一起. #include <stdio.h> #define STR1(x) #x//使x成为字符串 #define STR2(x, ...
- windows 配置msys2环境
msys2是一个在windows下模拟类unix的环境,之所以叫环境,是用为他提供了部分unix shell类似的功能,这个环境使你像在unix上使用shell一样.看到msys2你可能想到是不是还有 ...
- eclipse 集成 STS 插件
eclipse 集成 STS 插件 想新建一个 Spring Boot 工程,发现没有,如图:(展示的是集成之后的) eclipse 要和 sts 版本对应的,进入http://spring.io/t ...
- 【C++第一个Demo】---控制台RPG游戏4【角色系统】
[角色基类] #ifndef _ROLE_H_ #define _ROLE_H_ #include<list> #include<vector> #include " ...
- 2019 ACM-ICPC 南京 现场赛 H. Prince and Princess
题意 王子想要娶公主,但是需要完成一个挑战:在一些房间中找出公主在哪. 每个房间有一个人,他们彼此知道谁在哪个房间.可以问他们三种问题: 你是谁? 在某个房间是谁? 公主在哪个房间? 有三类人,一类一 ...