【算法】网络流-最大流(dinic)

【题解】

构图思路:

因为石柱高度是可以被消耗的,即一根石柱可通过的蜥蜴数量有限,取舍问题中这样表示容量的属性显然可以作为网络流中的边

于是将一根石柱拆成顶部和底部,中间连一条容量为石柱高度的边。

超级源向有蜥蜴的石柱顶连一条容量为1的边(表示一只蜥蜴)。

可以跳出地图的石柱底向超级汇连一条容量为inf的边。

d距离内的两根石柱连一条容量为inf的边。

记得结果要的是逃不掉的蜥蜴数。

edge数组开大!!!

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=,inf=0x3f3f3f3f;
struct edge{int from,v,flow;}e[];
int n,m,d,first[maxn],cur[maxn],q[],p[][],dt[maxn],S=,T=,tot=,ans=;
void insert(int u,int v,int w)
{tot++;e[tot].v=v;e[tot].flow=w;e[tot].from=first[u];first[u]=tot;
tot++;e[tot].v=u;e[tot].flow=;e[tot].from=first[v];first[v]=tot;}
bool bfs()
{
memset(dt,-,sizeof(dt));
int head=,tail=;q[]=S;dt[S]=;
while(head!=tail)
{
int x=q[head++];if(head>=)head=;
for(int i=first[x];i;i=e[i].from)
if(dt[e[i].v]==-&&e[i].flow>)
{
q[tail++]=e[i].v;if(tail>=)tail=;
dt[e[i].v]=dt[x]+;
}
}
if(dt[T]==-)return ;
return ;
}
int dinic(int x,int a)
{
if(x==T||a==)return a;
int flow=,f;
for(int& i=cur[x];i;i=e[i].from)
if(dt[e[i].v]==dt[x]+&&(f=dinic(e[i].v,min(a,e[i].flow)))>)
{
e[i].flow-=f;
e[i^].flow+=f;//tot从2开始!建反向弧!
flow+=f;
a-=f;
if(a==)break;
}
return flow;
}
int main()
{
scanf("%d%d%d",&n,&m,&d);
int cnt=;
char s[];
for(int i=;i<=n;i++)
{
scanf("%s",s+);
for(int j=;j<=m;j++)
{
cnt++;int u;
p[i][j]=cnt;
u=s[j]-'';
if(u)insert(cnt,cnt+,u);
}
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
for(int x=max(i-d,);x<=min(i+d,n);x++)
for(int y=max(j-d,);y<=min(j+d,m);y++)
if((i!=x||j!=y)&&(i-x)*(i-x)+(j-y)*(j-y)<=(d*d))
insert(p[i][j]+,p[x][y],inf);
for(int i=;i<=n;i++)
{
scanf("%s",s+);
for(int j=;j<=m;j++)
if(s[j]=='L')insert(S,p[i][j],),ans++;
}
for(int i=d+;i<=n-d;i++)
for(int j=;j<=d;j++)
{
insert(p[i][j]+,T,inf);
insert(p[i][m-j+]+,T,inf);
}
for(int i=;i<=d;i++)
for(int j=;j<=m;j++)
{
insert(p[i][j]+,T,inf);
insert(p[n-i+][j]+,T,inf);
}
while(bfs())
{
for(int i=;i<=;i++)cur[i]=first[i];
ans-=dinic(S,inf);
}
printf("%d",ans);
return ;
}

【BZOJ】1066: [SCOI2007]蜥蜴的更多相关文章

  1. POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)

    POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...

  2. BZOJ 1066: [SCOI2007]蜥蜴( 最大流 )

    结点容量..拆点然后随便写 --------------------------------------------------------------- #include<cstdio> ...

  3. [BZOJ 1066] [SCOI2007] 蜥蜴 【最大流】

    题目链接:BZOJ - 1066 题目分析 题目限制了高度为 x 的石柱最多可以有 x 只蜥蜴从上面跳起,那么就可以用网络流中的边的容量来限制.我们把每个石柱看作一个点,每个点拆成 i1, i2,从 ...

  4. poj 2711 Leapin' Lizards && BZOJ 1066: [SCOI2007]蜥蜴 最大流

    题目链接:http://poj.org/problem?id=2711 题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1066 Your p ...

  5. BZOJ 1066 [SCOI2007]蜥蜴(最大流)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1066 [题目大意] 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些 ...

  6. bzoj 1066: [SCOI2007] 蜥蜴

    这道题还是挺好想的,但我一开始还是想错了…… 把每个石柱拆成两个点,一个入度,一个出度,两个点连一条容量为高度的边,这样就可以限制从此石柱上经过的蜥蜴的数量.关于蜥蜴是否单独成点,我是单独当成了一个点 ...

  7. bzoj 1066 : [SCOI2007]蜥蜴 网络流

    题目链接 给一个n*m的图, 里面每一个点代表一个石柱, 石柱有一个高度. 初始时有些石柱上面有蜥蜴, 蜥蜴可以跳到距离他曼哈顿距离小于等于d的任意一个石柱上,跳完后, 他原来所在的石柱高度会减一, ...

  8. 1066: [SCOI2007]蜥蜴

    1066: [SCOI2007]蜥蜴 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3545  Solved: 1771[Submit][Status] ...

  9. 【BZOJ】1066: [SCOI2007]蜥蜴(最大流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1066 本题想一想应该懂了的. 我们想啊,,每个点都有限制,每个点都可以跳到另一个有限制的点,每个有蜥 ...

  10. 1066: [SCOI2007]蜥蜴 - BZOJ

    Description 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平 ...

随机推荐

  1. Java包名命名规则(转载)

    转载自:http://lilinhai548.blog.163.com/blog/static/5847332920155132151359/ 鸣谢原作者  学习Java的童鞋们都知道,Java的包. ...

  2. ci事务

    CI框架百问百答:CodeIgniter的事务用法?--第9问 时间 2013-06-06 10:57:45  CSDN博客 原文  http://blog.csdn.net/haor2756/art ...

  3. 使用vue的mixins混入实现对正在编辑的页面离开时提示

    mixins.ts import { Vue, Component, Watch } from "vue-property-decorator" Component.registe ...

  4. libnl3.2.25安装编译

    1,tar zxvf libnl-3.2.25.tar.gz 2,cd libnl-3.2.25 3,./configure --prefix=/usr  --sysconfdir=/etc  --d ...

  5. zookeeper学习之集群环境搭建

    一.安装环境 zookeeper:3.4.6 JDK:1.8 linux:centos6.5  64位 主机: server0:192.168.0.224server1:192.168.0.225se ...

  6. apache server-status配置

    引言 自己配置LAMP服务器时(xwamp),获取状态信息出现错误: You don't have permission to access /server-status on this server ...

  7. 20165218 2017-2018-2 《Java程序设计》课程总结

    20165218 2017-2018-2 <Java程序设计>课程总结 一.每周作业链接汇总 20165218 我期望的师生关系 20165218 学习基础和C语言基础调查 2016521 ...

  8. centos 6.5 web service模式 系统 安装php

    1 首先是坚持libxml2出问题,提示如下: checking for xml2-config path... /home/www/thirdlib/libxml2/bin/xml2-configc ...

  9. bzoj5118: Fib数列2(费马小定理+矩阵快速幂)

    题目大意:求$fib(2^n)$ 就是求fib矩阵的(2^n)次方%p,p是质数,根据费马小定理有 注意因为模数比较大会爆LL,得写快速乘法... #include<bits/stdc++.h& ...

  10. move_base的全局路径规划代码研究

    algorithmn parameter code 主要是以下三个函数 计算所有的可行点 怎么计算一个点的可行点 从可行点中计算路径path todo algorithmn 算法的解释 Dijkstr ...