您的位置:首页 > 其它

HDU 2732 Leapin' Lizards(最大流)

2015-10-24 19:57 281 查看

Leapin' Lizards

[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
The input file will begin with a line containing a single integer representing the number of test cases, which is at most 25. Each test case will begin with a line containing a single positive integer n representing the number of rows in the map, followed by a single non-negative integer d representing the maximum leaping distance for the lizards. Two maps will follow, each as a map of characters with one row per line. The first map will contain a digit (0-3) in each position representing the number of jumps the pillar in that position will sustain before collapsing (0 means there is no pillar there). The second map will follow, with an 'L' for every position where a lizard is on the pillar and a '.' for every empty pillar. There will never be a lizard on a position where there is no pillar.Each input map is guaranteed to be a rectangle of size n x m, where 1 ≤ n ≤ 20 and 1 ≤ m ≤ 20. The leaping distance is
always 1 ≤ d ≤ 3.

[align=left]Output[/align]
For each input case, print a single line containing the number of lizards that could not escape. The format should follow the samples provided below.

[align=left]Sample Input[/align]

4

3 1
1111

1111
1111
LLLL
LLLL
LLLL
3 2

00000
01110

00000

.....
.LLL.
.....

3 1

00000
01110
00000

.....

.LLL.
.....
5 2
00000000
02000000

00321100
02000000
00000000
........

........
..LLLL..

........
........

[align=left]Sample Output[/align]

Case #1: 2 lizards were left behind.

Case #2: no lizard was left behind.

Case #3: 3 lizards were left behind.

Case #4: 1 lizard was left behind.

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;

struct Edge
{
int from,to,cap,flow;
};
const int maxn=1005;
const int inf=0x3f3f3f;

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 num)
{
for(int i=0;i<num;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});
int 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.cap>e.flow)
{
vis[e.to]=1;
d[e.to]=d[x]+1;
q.push(e.to);
}
}
}
return vis[t];
}

int dfs(int x,int a)
{
if(x==t||a==0)return a;
int flow=0,f;
for(int& i=cur[x];i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
{
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
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;
}
}dc;

int main()
{
int T;
scanf("%d",&T);
for(int kase=1;kase<=T;kase++)
{
int m,n,d,sum=0;
scanf("%d%d",&n,&d);
for(int i=0;i<n;i++)
{
string s;
cin>>s;
if(i==0)m=s.size(),dc.init(2*n*m+2);
for(int j=0;j<m;j++)
{
int id=i*m+j+1;
if(s[j]-'0'>0)
{
dc.addedge(id,id+m*n,s[j]-'0');
if(i<d||i+d>=n||j<d||j+d>=m)
dc.addedge(id+n*m,2*n*m+1,inf);
else
{
for(int k=0;k<n;k++)
for(int l=0;l<m;l++)
{
int id2=k*m+l+1;
if(id==id2)continue;
if(abs(i-k)+abs(j-l)<=d)
dc.addedge(id+n*m,id2,inf);
}
}
}
}
}
for(int i=0;i<n;i++)
{
string s;
cin>>s;
for(int j=0;j<s.size();j++)
if(s[j]=='L')
{
sum++;
dc.addedge(0,i*m+j+1,1);
}
}
int ans=sum-dc.maxflow(0,2*m*n+1);
if(ans==0) printf("Case #%d: no lizard was left behind.\n",kase);
else if(ans==1) printf("Case #%d: 1 lizard was left behind.\n",kase);
else printf("Case #%d: %d lizards were left behind.\n",kase,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: