HDU 2732 Leapin' Lizards
网络最大流+拆点。输出有坑!!!
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=+;
const int INF=0x7FFFFFFF; struct Edge
{
int from,to,cap,flow;
};
vector<Edge>edges;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
int n,m,s,t; //求出层次网络
bool BFS()
{
memset(vis,,sizeof(vis));
queue<int>Q;
Q.push(s);
d[s]=;
vis[s]=;
while(!Q.empty())
{
int x=Q.front();
Q.pop();
for(int i=; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=;
d[e.to]=d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
} //加边
void AddEdge(int from,int to,int cap)
{
Edge r;
r.from=from;
r.to=to;
r.cap=cap;
r.flow=;
edges.push_back(r);
Edge d;
d.from=to;
d.to=from;
d.cap=;
d.flow=;
edges.push_back(d);
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} //每个阶段来一次DFS增广
int DFS(int x,int a)
{
if(x==t||a==) return a;
int flow=,f;
for(int i=cur[x]; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
e.flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
return flow;
} //多个阶段,多次建立层次网络。
int Maxflow(int ss,int tt)
{
int flow=;
while(BFS())
{
memset(cur,,sizeof(cur));
flow+=DFS(ss,INF);
}
return flow;
} int N,M,D,Tot;
char Map1[][],Map2[][];
int B[][];
struct Zhu
{
int x,y;
int w;
} zhu[]; int main()
{
int TTT;
scanf("%d",&TTT);
for(int YYY=;YYY<=TTT;YYY++)
{
scanf("%d%d",&N,&D);
memset(B,,sizeof(B));
for(int i=; i<maxn; i++) G[i].clear();
edges.clear();
Tot=;
for(int i=; i<N; i++) scanf("%s",Map1[i]);
for(int i=; i<N; i++) scanf("%s",Map2[i]);
M=strlen(Map1[]); for(int i=; i<N; i++)
for(int j=; j<M; j++)
if(Map1[i][j]!='')
{
zhu[Tot].x=i;
zhu[Tot].y=j;
zhu[Tot].w=Map1[i][j]-'';
B[i][j]=Tot;
Tot++;
}
s=;
t=;
Tot--;
for(int i=; i<=Tot; i++)
{
int X=zhu[i].x,Y=zhu[i].y;
AddEdge(i,i+Tot,zhu[i].w);
if(min(min(X+,N-X),min(Y+,M-Y))<=D)
AddEdge(i+Tot,t,INF);
} for(int i=; i<=Tot; i++)
for(int j=; j<=Tot; j++)
{
if(i==j) continue;
int Ju=abs(zhu[i].x-zhu[j].x)+abs(zhu[i].y-zhu[j].y);
if(Ju<=D) AddEdge(i+Tot,j,INF);
}
int FF=;
for(int i=; i<N; i++)
for(int j=; j<M; j++)
if(Map2[i][j]=='L')
{
AddEdge(s,Tot+Tot+FF,);
AddEdge(Tot+Tot+FF,B[i][j],);
FF++;
}
FF--;
printf("Case #%d: ",YYY);
int ANS=FF-Maxflow(s,t);
if(ANS==) printf("no lizard was left behind.\n");
else if(ANS==) printf("1 lizard was left behind.\n");
else printf("%d lizards were left behind.\n",ANS);
}
return ;
}
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(最大流)
http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意: 给出n行的网格,还有若干只蜥蜴,每只蜥蜴一开始就在一个格子之中,并且给出蜥蜴每次的最大跳跃长度d. ...
- hdu 2732 Leapin' Lizards(最大流)Mid-Central USA 2005
废话: 这道题不难,稍微构造一下图就可以套最大流的模板了.但是我还是花了好久才解决.一方面是最近确实非常没状态(托词,其实就是最近特别颓废,整天玩游戏看小说,没法静下心来学习),另一方面是不够细心,输 ...
- HDU - 2732 Leapin' Lizards (拆点最大流)
题意:有N*M的矩形,每个格点有一个柱子,每根柱子有高度c,允许蜥蜴经过这根柱子c次,开始有一些蜥蜴在某些柱子上,它们要跳出这个矩形,每步最大能跳d个单位,求最少有多少蜥蜴不能跳出这个矩形. 分析:转 ...
- 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(拆点+最大流)
HDU 2732 Leapin' Lizards 题目链接 题意:有一些蜥蜴在一个迷宫里面,有一个跳跃力表示能跳到多远的柱子,然后每根柱子最多被跳一定次数,求这些蜥蜴还有多少是不管怎样都逃不出来的. ...
- 【HDOJ】2732 Leapin' Lizards
贪心+网络流.对于每个结点,构建入点和出点.对于每一个lizard>0,构建边s->in position of lizard, 容量为1.对于pillar>0, 构建边in pos ...
- 【解题报告】 Leapin' Lizards HDU 2732 网络流
[解题报告] Leapin' Lizards HDU 2732 网络流 题外话 在正式讲这个题目之前我想先说几件事 1. 如果大家要做网络流的题目,我在网上看到一个家伙,他那里列出了一堆网络流的题目, ...
随机推荐
- url地址数据转换成json数据格式
var urlToJson = function(){ var ret = {}; window.location.search.substr(1).replace(/(\w+)=(\w+)/ig, ...
- 【微服务轻量化容器技术相关】同事分享的Docker学习汇总
还没时间去练习: Docker commands: docker images (list all image on this host) docker ps -a ( list all contai ...
- redis13---事务处理。
Jedis事务我们使用JDBC连接Mysql的时候,每次执行sql语句之前,都需要开启事务:在MyBatis中,也需要使用openSession()来获取session事务对象,来进行sql执行.查询 ...
- LINQ To SQL 处理 DateTime?
LINQ To SQL 处理 DateTime? 类型 例子: 搜索栏含有最后扫描时间的日期(DateTime?)与多个其他条件(String) 现在需要写一个查询 : 查询符合最后扫描的日期的查询 ...
- oracle索引
1,建立索引 create index goods_num on goods (num) tablespace data; 2,查看表上索引 select * from USER_INDEXES wh ...
- ZOJ 1003 Crashing Balloon
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using ...
- hdu1025
#include<stdio.h>const int MAXN=500010;int a[MAXN],b[MAXN]; //用二分查找的方法找到一个位置,使得num>b[i-1] 并 ...
- Python编码问题整理【转】
认识常见编码 GB2312是中国规定的汉字编码,也可以说是简体中文的字符集编码 GBK 是 GB2312的扩展 ,除了兼容GB2312外,它还能显示繁体中文,还有日文的假名 cp936:中文本地系统是 ...
- 《JS权威指南学习总结--第7章 数组》
内容要点: 一. JS数组是无类型的:数组元素可以是任意对象,并且同一个数组中的不同元素也可能有不同的类型.数组的元素甚至也可能是对象或其他属性,这允许创建复制的数据结构,如对象的数组和数组的数组. ...
- PAT 团体程序设计天梯赛-练习集 L1-020. 帅到没朋友
原题 https://www.patest.cn/contests/gplt/L1-020 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友.本题就要求你找出那些帅到没有朋友的人. ...