题意:有N*M的矩形,每个格点有一个柱子,每根柱子有高度c,允许蜥蜴经过这根柱子c次,开始有一些蜥蜴在某些柱子上,它们要跳出这个矩形,每步最大能跳d个单位,求最少有多少蜥蜴不能跳出这个矩形。

分析:转化为求最多有多少蜥蜴能跳出,则变为最大流问题。经典的建图思路,将每个柱子视作点,将其拆为入点和出点,入点到出点建一条容量为c的边,若一个点距离距离边界小于d,建边。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define mp(x,y) make_pair(x,y)
typedef pair<int,int> PII; const int INF = 0x3f3f3f3f;
const int maxn = 1e3+5; bool vis[maxn];
struct Edge{
int from, to,cap,flow;
};
int cost[maxn];
struct Dinic
{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn]; void init(int n){
this->n = n;
for(int i=0;i<=n;++i){
G[i].clear();
}
edges.clear();
} void AddEdge(int from,int to,int cap){
edges.push_back((Edge){from,to,cap,0});
edges.push_back((Edge){to,from,0,0});
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
} bool BFS(){
memset(vis,0,sizeof(vis));
queue<int> q;
q.push(s);
d[s] = 0;
vis[s] = 1;
while(!q.empty()){
int x = q.front(); q.pop();
for(int i=0;i<G[x].size();i++){
Edge &e = edges[G[x][i]];
if(!vis[e.to] && e.flow < e.cap){
vis[e.to] = 1;
d[e.to] = d[x] + 1;//层次图
q.push(e.to);
}
}
}
return vis[t];//能否到汇点,不能就结束
}
int DFS(int x,int a)//x为当前节点,a为当前最小残量
{
if(x == t || a == 0) return a;
int flow = 0 , r;
for(int& i = cur[x];i < G[x].size();i++){
Edge& e = edges[G[x][i]];
if(d[x] + 1 == d[e.to] && (r = DFS(e.to , min(a,e.cap - e.flow) ) ) > 0 ){
e.flow += r;
edges[G[x][i] ^ 1].flow -= r;
flow += r;//累加流量
a -= r;
if(a == 0) break;
}
}
return flow;
}
int MaxFlow(int s,int t){
this->s = s;
this->t = t;
int flow = 0;
while(BFS()){
memset(cur,0,sizeof(cur));
flow += DFS(s,INF);
}
return flow;
}
}F;
char str[maxn];
int G[22][22];
int h,w,d; bool out(int x,int y)
{
if(x<0||x>=h||y<0||y>=w) return true;
return false;
} void build(int x,int y)
{
int all = w*h;
int ed = 2*w*h+1;
int u = x*w+y; F.AddEdge(x*w+y,x*w+y+all,G[x][y]); if(x-d<0 ||x+d>=h||y-d<0 ||y+d>=w){
F.AddEdge(u+all,ed,INF);
return;
}
for(int i=0;i<h;++i){
for(int j=0;j<w;++j){
if(i==x && j==y) continue;
if(abs(x-i)+abs(y-j)<=d){
F.AddEdge(u+all,i*w+j,INF);
}
}
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int N,M,T,cas=1; scanf("%d",&T);
while(T--){
int u,v,tmp,base;
int st,ed;
scanf("%d %d",&h,&d);
for(int i=0;i<h;++i){
scanf("%s",str);
if(i==0){
w = strlen(str);
base = w*h;
st = 2*h*w,ed = st+1;
F.init(ed+1);
}
for(int j=0;j<w;++j){
G[i][j] = (int)(str[j]-'0');
}
} for(int i=0;i<h;++i){
for(int j=0;j<w;++j){
if(!G[i][j]) continue;
build(i,j);
}
} int tot = 0;
for(int i=0;i<h;++i){
scanf("%s",str);
for(int j=0;j<w;++j){
char c = str[j];
if(c=='L'){
tot++;
u = i*w+j;
F.AddEdge(st,u,1); //源点加边
}
}
}
int res= tot - F.MaxFlow(st,ed);
printf("Case #%d: ",cas++);
if(!res) printf("no lizard was left behind.\n");
else if(res==1) printf("1 lizard was left behind.\n");
else printf("%d lizards were left behind.\n",res);
}
return 0;
}

HDU - 2732 Leapin' Lizards (拆点最大流)的更多相关文章

  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. hdu 2732 Leapin' Lizards 最大流 拆点 建图

    题目链接 题意 给定一张网格,格子中有些地方有柱子,有些柱子上面有蜥蜴. 每个柱子只能承受有限只蜥蜴从上面经过.每只蜥蜴每次能走到相距曼哈顿距离\(\leq k\)的格子中去. 问有多少只蜥蜴能走出网 ...

  3. hdu 2732 Leapin' Lizards (最大流 拆点建图)

    Problem Description Your platoon of wandering lizards has entered a strange room in the labyrinth yo ...

  4. HDU 2732 Leapin' Lizards(最大流)

    http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意: 给出n行的网格,还有若干只蜥蜴,每只蜥蜴一开始就在一个格子之中,并且给出蜥蜴每次的最大跳跃长度d. ...

  5. hdu 2732 Leapin' Lizards(最大流)Mid-Central USA 2005

    废话: 这道题不难,稍微构造一下图就可以套最大流的模板了.但是我还是花了好久才解决.一方面是最近确实非常没状态(托词,其实就是最近特别颓废,整天玩游戏看小说,没法静下心来学习),另一方面是不够细心,输 ...

  6. HDU 2732 Leapin' Lizards

    网络最大流+拆点.输出有坑!!! #include<cstdio> #include<cstring> #include<string> #include<c ...

  7. HDU 2732 Leapin&#39; Lizards(拆点+最大流)

    HDU 2732 Leapin' Lizards 题目链接 题意:有一些蜥蜴在一个迷宫里面,有一个跳跃力表示能跳到多远的柱子,然后每根柱子最多被跳一定次数,求这些蜥蜴还有多少是不管怎样都逃不出来的. ...

  8. 【HDOJ】2732 Leapin' Lizards

    贪心+网络流.对于每个结点,构建入点和出点.对于每一个lizard>0,构建边s->in position of lizard, 容量为1.对于pillar>0, 构建边in pos ...

  9. HDU 4780 Candy Factory(拆点费用流)

    Problem Description   A new candy factory opens in pku-town. The factory import M machines to produc ...

随机推荐

  1. 各种API中文文档下载地址

    转发: http://www.aseoe.com/api-download/download.html jquery easyui 帮助文档: http://download.csdn.net/dow ...

  2. ArcGIS GP应用-GP模型服务发布

    1.双击模型名称打开运行窗体 2.在图上交互和窗体中输入数据后,点击确定运行模型,查看运行结果 3.在当前模型“缓冲区分析”的目录树上,右击含有图形(点.线.面)的节点,点击添加至显示,显示图片 4. ...

  3. IOS实现打电话后回调

    本文转载至 http://blog.csdn.net/cerastes/article/details/38340687   UIWebView *callWebview =[[UIWebView a ...

  4. Java中关于枚举的7种用法

    1.定义常量: public enum Color { RED,ORANGE,YELLOW,GREEN,INDIGO,BLUE,PURPLE } 2.用于switch: enum Color { RE ...

  5. poj2396 Budget&&ZOJ1994 Budget[有源汇上下界可行流]

    Budget Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge We are supposed to make ...

  6. 170328、Maven+SpringMVC+Dubbo 简单的入门demo配置

    之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程序猿来说,真是一个好消息.最近有时间了,打算做一个demo把dubbo在本地跑起来先. 先copy一段dubbo ...

  7. SharePoint Managed Metadata 使用总结

    前言 本文完全原创,转载请说明出处,希望对大家有用. 在SharePoint开发中,通常我们会将数据存储在列表,文档库或者直接存到数据库.但涉及到数据的层级结构时,用列表等存储实现并不是一件简单的事情 ...

  8. xpath scrapy shell

    w from scrapy.spider import Spider from scrapy.crawler import CrawlerProcess import pymysql conn = p ...

  9. 学习IPFS

    注:以下所有操作均在CentOS 6.8 x86_64位系统下完成. IPFS(InterPlanetary File System)是一个点对点的分布式超媒体分发协议,被认为是最有可能取代HTTP的 ...

  10. Windows10安装MySQL5.6.24

    1.解压安装包到指定目录如:E:\Java\mysql-5.6-24-win32 2.在E:\Java\mysql-5.6-24-win32目录下新建my.ini文件,内容如下 [mysqld] ba ...