POJ 2711 Leapin' Lizards (网络流,拆点,Dinic)

Leapin' Lizards(蜥蜴)

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1863 Accepted: 726

Description

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.

Input

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.

Output

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.

Sample Input

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..
........
........

Sample Output

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.

Hint

Brute force methods examining every path will likely exceed the allotted time limit.

Source

Mid-Central USA 2005

求网格中能不能跳出的蜥蜴

我们可以用dinic 先求出 能跳出 的 然后 all - 能跳出的 =ans

建图:(难哭)来自:https://www.cnblogs.com/agenthtb/p/7512481.html

源点S为0,拆网格的每一个点,拆成 i 和 i+cnt(cnt:为网格的总个数,i为网格中的蜥蜴,i+n*m表示蜥蜴出去) 汇点T = cnt *2 +1;

如果格子i上有蜥蜴,那么从S到i有边(S,i,1)

如果格子i能承受x次跳出 那么 有边 (i,i+cnt,x);

如果从格子i能直接跳出网格边界,那么有边(i+cnt,t,inf)

如果从格子i不能直接跳出网格,那么 从 i 到 离 i 距离 <=d 的网格 j 右边 (i+cnt,j,inf);

注意这里的距离是 abs(行号差)+ abs(列号差)

Dinic 最终求的最大流就是 跳出网格的蜥蜴数

网络流的拆点操作是为了解决有些题目要求每个点的选择次数是有限制的.

比如 id 这个点的被选择次数最大是3,那么我们把它拆成id1 跟 id2两个点分别来表示入id和出id

再在id1和id2之间建一条容量为3的边,这样每次进入id的时候我们就可以让它其实进入id1然后原地跳一下跳到id2这样就控制了某个点的选择的次数

/*
t组案例
n行 d跳跃的最大数
*/
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <cstdio>
#include <cmath>
#pragma comment (linker,"/STACK:102400000,102400000")
#define mem(a,b) memset(a,b,sizeof(a));
#define pi acos(-1.0);
#define eps 1e-6
#define ll long long
#define maxx 100000
#define inf 0x3f3f3f3f
using namespace std;
struct Edge
{
    int u,v,cap,next;
}edge[maxx];
int n,d,dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
int head[maxx],level[maxx],cur[maxx];
int num,cnt,all;
char mapp[30][30];
int Mapp[30][30];
int Num[30][30];
void init()
{
    cnt=0;
    num=0;
    all=0;
    mem(head,-1);
}
void add_edge(int u,int v,int w)
{
    edge[num].u=u;
    edge[num].v=v;
    edge[num].cap=w;
    edge[num].next=head[u];
    head[u]=num++;

    edge[num].u=v;
    edge[num].v=u;
    edge[num].cap=0;
    edge[num].next=head[v];
    head[v]=num++;
}
int bfs(int s,int t)
{
    mem(level,0);
    queue<int >q;
    level[s]=1;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(level[v]==0&&edge[i].cap>0)
            {
                level[v]=level[u]+1;
                q.push(v);
            }
        }
    }return level[t]!=0;
}
int dfs(int u,int t,int f)
{
    if(u==t) return f;
    for(int &i=cur[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(edge[i].cap>0&&level[v]==level[u]+1)
        {
            int d=dfs(v,t,min(f,edge[i].cap));
            if(d>0)
            {
                edge[i].cap-=d;
                edge[i^1].cap+=d;
                return d;
            }
        }
    }return 0;
}
int dinic(int s,int t,int cnt)
{
    int flow=0;
    while(bfs(s,t))
    {
        for(int i=0;i<=cnt;i++) cur[i]=head[i];
        int f;
        while((f=dfs(s,t,inf))>0)
            flow+=f;
    }
    return flow;
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int T=1;T<=t;T++)
    {
        init();
        int len;
        scanf("%d%d",&n,&d);
        for(int i=0;i<n;i++)
        {
            scanf("%s",mapp[i]);
            len=strlen(mapp[i]);
            for(int j=0;j<len;j++)
            {
                Mapp[i][j]=mapp[i][j]-'0';
                Num[i][j]=++cnt;//计数看有多少个网格
            }
        }
        mem(mapp,0);
        for(int i=0;i<n;i++)
        {
            scanf("%s",mapp[i]);
        }
        int t=2*cnt+1;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<len;j++)
            {
                if(Mapp[i][j])
                {
                    if(i<d||j<d||n-i<=d||len-j<=d)//可以直接跳出去的 直接与汇点相连 inf
                        add_edge(Num[i][j]+cnt,t,inf);
                    add_edge(Num[i][j],Num[i][j]+cnt,Mapp[i][j]);//拆点
                    for(int k=0;k<n;k++)
                    {
                        for(int p=0;p<len;p++)
                        {
                            int dx=abs(i-k);
                            int dy=abs(j-p);
                            double mm=sqrt(dx*dx*1.0+dy*dy*1.0);
                            if(mm>d) continue;
                            add_edge(Num[i][j]+cnt,Num[k][p],inf);
                        }
                    }
                }
                if(mapp[i][j]=='L')
                {
                    add_edge(0,Num[i][j],1);
                    all++;
                }
            }
        }
        int s=dinic(0,t,t+1);
        int ans=all-s;
        if(!ans)
            printf("Case #%d: no lizard was left behind.\n",T);
        else if(ans==1)
            printf("Case #%d: 1 lizard was left behind.\n",T);
        else
            printf("Case #%d: %d lizards were left behind.\n",T,ans);
    }return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值