HDU - 2732 Leapin' Lizards (拆点最大流)
题意:有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 (拆点最大流)的更多相关文章
- POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)
POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...
- hdu 2732 Leapin' Lizards 最大流 拆点 建图
题目链接 题意 给定一张网格,格子中有些地方有柱子,有些柱子上面有蜥蜴. 每个柱子只能承受有限只蜥蜴从上面经过.每只蜥蜴每次能走到相距曼哈顿距离\(\leq k\)的格子中去. 问有多少只蜥蜴能走出网 ...
- hdu 2732 Leapin' Lizards (最大流 拆点建图)
Problem Description Your platoon of wandering lizards has entered a strange room in the labyrinth yo ...
- HDU 2732 Leapin' Lizards(最大流)
http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意: 给出n行的网格,还有若干只蜥蜴,每只蜥蜴一开始就在一个格子之中,并且给出蜥蜴每次的最大跳跃长度d. ...
- hdu 2732 Leapin' Lizards(最大流)Mid-Central USA 2005
废话: 这道题不难,稍微构造一下图就可以套最大流的模板了.但是我还是花了好久才解决.一方面是最近确实非常没状态(托词,其实就是最近特别颓废,整天玩游戏看小说,没法静下心来学习),另一方面是不够细心,输 ...
- HDU 2732 Leapin' Lizards
网络最大流+拆点.输出有坑!!! #include<cstdio> #include<cstring> #include<string> #include<c ...
- HDU 2732 Leapin' Lizards(拆点+最大流)
HDU 2732 Leapin' Lizards 题目链接 题意:有一些蜥蜴在一个迷宫里面,有一个跳跃力表示能跳到多远的柱子,然后每根柱子最多被跳一定次数,求这些蜥蜴还有多少是不管怎样都逃不出来的. ...
- 【HDOJ】2732 Leapin' Lizards
贪心+网络流.对于每个结点,构建入点和出点.对于每一个lizard>0,构建边s->in position of lizard, 容量为1.对于pillar>0, 构建边in pos ...
- HDU 4780 Candy Factory(拆点费用流)
Problem Description A new candy factory opens in pku-town. The factory import M machines to produc ...
随机推荐
- linux_shell_find命令
使用find查找文件 基本格式:find path expression 1.按照文件名查找 (1)find / -name httpd.conf #在根目录下查找文件httpd.conf,表示在整个 ...
- Struts2_day03--向值栈放数据
向值栈放数据 1 向值栈放数据多种方式 第一种 获取值栈对象,调用值栈对象里面的 set 方法 第二种 获取值栈对象,调用值栈对象里面的 push方法 第三种 在action定义变量,生成变量的ge ...
- Struts2_day01--导入源文件_Struts2的执行过程_查看源代码
导入源文件 选中按ctrl + shift + t进入 Struts2执行过程 画图分析过程 过滤器在服务器启动时创建,servlet在第一次访问时创建 查看源代码 public class Stru ...
- JSP内置对象——request 及其响应get和post请求的实例
request对象客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例.request对象具有请求域,即完成客户端的 ...
- Excel 一个工作表进行按行数拆分
1. 如下Excel表,总共有120多行数据,如何将以50行数据为一个工作表进行拆分 Sub ZheFenSheet() Dim r, c, i, WJhangshu, WJshu, bt As Lo ...
- IOS模拟器
IOS模拟器 目录 概述 实用操作 概述 实用操作 快速删除大量程序的方式 菜单栏 -> Reset Contain And Settings 或者:直接删除模拟器应用里面的想要去除的应用程序的 ...
- poj3666 Making the grade【线性dp】
Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total Submissions:10187 Accepted: 4724 ...
- TOMCAT------>web资源访问
1.web应用达成war包 通过命令行打war包:jar -cvf xxx.war xxx 因为放到webapps里电脑会自动识别,自动解压 2.relodeable="true" ...
- 20165330 2017-2018-2 《Java程序设计》第5周学习总结
课本知识总结 第七章 内部类与异常类 内部类:类的一种成员 外嵌类:包含内部类的类称为内部类的外嵌类 二者关系: 内部类的外嵌类的成员变量在内部类中仍然有效,内部类中的方法也可以调用外嵌类中的方法. ...
- 设计模式之Factory工厂模式
在上一章,模板模式中,我们在父类规定处理的流程,在子类中实现具体的处理.如果我们将该模式用于生成实例,便演变成了Factory模式,即工厂模式. 在Factory模式中,父类决定实例的生成方式,但并不 ...