题目链接:http://poj.org/problem?id=2711
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1066
Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasures, one of the rookies steps on an innocent-looking stone and the room's floor suddenly disappears! Each lizard in your platoon is left standing on a fragile-looking pillar, and a fire begins to rage below...
Leave no lizard behind! Get as many lizards as possible out of the room, and report the number of casualties.
The pillars in the room are aligned as a grid, with each pillar one unit away from the pillars to its east, west, north and south. Pillars at the edge of the grid are one unit away from the edge of the room (safety). Not all pillars necessarily have a lizard. A lizard is able to leap onto any unoccupied pillar that is within d units of his current one. A lizard standing on a pillar within leaping distance of the edge of the room may always leap to safety... but there's a catch: each pillar becomes weakened after each jump, and will soon collapse and no longer be usable by other lizards. Leaping onto a pillar does not cause it to weaken or collapse; only leaping off of it causes it to weaken and eventually collapse. Only one lizard may be on a pillar at any given time.
题意描述:在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外。 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平面距离不超过d的任何一个石柱上。石柱都不稳定,每次当蜥蜴跳跃时,所离开的石柱高度减1(如果仍然落在地图内部,则到达的石柱高度不变),如果该石柱原来高度为1,则蜥蜴离开后消失。以后其他蜥蜴不能落脚。任何时刻不能有两只蜥蜴在同一个石柱上。
算法分析:新增源点from和汇点to,每个位置u拆边u->r*c+u(如果位置上没石柱,权值为inf,否则权值为石柱的高度);石柱之间如果距离不超过D,连边并权值为无穷大;
from连边有蜥蜴的位置(点拆分后的前驱点,权值为1);如果点的位置距离边界外不超过D,则该点拆分后的后继点连边汇点to,权值为inf。最大流解之即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int M = +; int r,c,from,to;
int D;
struct node
{
int v,flow;
int next;
}edge[M*];
int head[maxn],edgenum; void add(int u,int v,int flow)
{
edge[edgenum].v=v ;edge[edgenum].flow=flow ;
edge[edgenum].next=head[u] ;head[u]=edgenum++ ; edge[edgenum].v=u ;edge[edgenum].flow= ;
edge[edgenum].next=head[v] ;head[v]=edgenum++ ;
} int d[maxn];
int bfs()
{
memset(d,,sizeof(d));
d[from]=;
queue<int> Q;
Q.push(from);
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (!d[v] && edge[i].flow)
{
d[v]=d[u]+;
Q.push(v);
if (v==to) return ;
}
}
}
return ;
} int dfs(int u,int flow)
{
if (u==to || flow==) return flow;
int cap=flow;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (d[v]==d[u]+ && edge[i].flow)
{
int x=dfs(v,min(edge[i].flow,cap));
edge[i].flow -= x;
edge[i^].flow += x;
cap -= x;
if (cap==) return flow;
}
}
return flow-cap;
} int dinic()
{
int sum=;
while (bfs()) sum += dfs(from,inf);
return sum;
} int main()
{
char str[][];
char str2[][];
int ncase=;
int t;
scanf("%d",&t);
while (t--)
{
memset(str,,sizeof(str));
scanf("%d%d",&r,&D);
for (int i= ;i<=r ;i++)
scanf("%s",str[i]+);
c=strlen(str[]+);
memset(head,-,sizeof(head));
edgenum=;
from=*r*c+;
to=from+;
for (int i= ;i<=r ;i++)
{
for (int j= ;j<=c ;j++)
if (str[i][j]!='')
{
for (int u= ;u<=r ;u++)
{
for (int v= ;v<=c ;v++)
{
if (u==i && v==j) continue;
if (str[u][v]=='') continue;
int dis=abs(u-i)+abs(v-j);
if (dis<=D) {
add(r*c+(i-)*c+j,(u-)*c+v,inf);
}
}
}
}
}
memset(str2,,sizeof(str2));
for (int i= ;i<=r ;i++) scanf("%s",str2[i]+);
int ans=;
for (int i= ;i<=r ;i++)
{
for (int j= ;j<=c ;j++)
{
if (str2[i][j]=='.') continue;
ans ++ ;
add(from,(i-)*c+j,);
}
}
for (int i= ;i<=r ;i++)
{
for (int j= ;j<=c ;j++)
{
int num=str[i][j]-'';
add((i-)*c+j,r*c+(i-)*c+j,num== ? inf : num);
if (i<=D || j<=D || (r-i)<=D- || (c-j)<=D-)
add(r*c+(i-)*c+j,to,inf);
}
}
//cout<<ans<<endl;
int sum=ans-dinic();
printf("Case #%d: ",ncase++);
if (sum==) printf("no lizard was left behind.\n");
else if (sum==) printf("1 lizard was left behind.\n");
else printf("%d lizards were left behind.\n",sum);
}
return ;
}