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 ...
随机推荐
- sizeof 数组与指针
在学习指针的时候,得到指针的定义和数组的定义一样,但是这时候就很好奇,指针只是一个地址,那数组和指针一样的话,sizeof时怎么得知其长度呢. 于是百度了下面的回复: 千万不要把数组名看成指针,尽管有 ...
- Redis异构集群数据在线迁移工具Redis-Migrate-Tool【转】
摘要:Redis-Migrate-Tool(后面都简称RMT),是唯品会开源的redis数据迁移工具,主要用于异构redis集群间的数据在线迁移,即数据迁移过程中源集群仍可以正常接受业务读写请求,无业 ...
- UE打包32位程序遇到Win32 is not a supported platform for MindWaveEditor. Valid platforms are Win64.
1>------ 已启动全部重新生成: 项目: MindWave, 配置: Development_Editor Win32 ------1> Win32 is not a support ...
- PE导入表分析
A.dll 导入 B.dll 导出函数 A.dll 表内容 这个结构指向的B导出函数的地址 Hook这个位置 等同于 Hook B.dll导出函数
- tomcat自动加载class
转载 tomcat自动加载改变的class文件(无需重启tomcat) http://blog.csdn.net/miraclestar/article/details/6434164 不重启Tom ...
- Go语言中的一些函数
1.并行 通过使用goroutine和channel,go语言可以很好地支持并发,但是在我的电脑上是默认只使用一个核执行,要使用多核,在代码前面加入 import("runtime" ...
- centos安装Oracle virtual box
1.进入virtualbox官网 https://www.virtualbox.org/wiki/Downloads 2.点击Linux distributions 3.向下翻至如图 4.在/etc/ ...
- JavaScript 禁止表单提交
有时我们需要在表单真正提交之前,做一些检查工作,检查通过之后再进行提交. <form name="myForm" onsubmit = "validateMyFor ...
- PAT 甲级 1068 Find More Coins(0,1背包)
1068. Find More Coins (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva l ...
- 【Python Programe】使用Python发送语音验证
使用Python向手机发送语音验证码,需要工具有: virtualenv 创建独立运行环境 Twilio 帐号去调用相应的API Twilio 的python库,5.7.0版本 1.使用 virtua ...