hdu2732 Leapin' Lizards(最大流)

Leapin' Lizards

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3266    Accepted Submission(s): 1342


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

题意:给出两个长度为n的图,宽度未知,每次蜥蜴最大跳跃距离为d

第一个图,给出了每个柱子能被跳跃的次数,为0表示没柱子

第二个图,给出了刚开始蜥蜴所在的位置

当蜥蜴跳出这张图则视为安全,问最少有多少个蜥蜴跳不出去

—————————————————————分割线————————————————————————————


有很多边并限制流量,求最大流量,最大流问题

首先考虑如何建边

为了限制每个柱子的流量,需要将柱子拆点

1.左柱与右柱建边,流量为柱子的可跳次数

2.将每个柱子与他能够跳到的柱子建边,容量为INF;

3.将源点与有蜥蜴的柱子建边,容量为1

4.将可以跳出的柱子和汇点建边,容量为INF;

然后上最大流模板

注意坑点:单词为单数不加s,负数加s,还有be动词也要改(我是没注意到TwT)

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=5000+50;
const int maxm=1e7+50;
struct node
{
    node() {};
    node(int tv,int tw,int tnext)
    {
        v=tv,w=tw,next=tnext;
    };
    int v,w,next;
} e[maxm];
int first[maxn],vis[maxn],dis[maxn],tot;
void add_edge(int u,int v,int w)
{
    e[tot]=node(v,w,first[u]);
    first[u]=tot++;
    e[tot]=node(u,0,first[v]);
    first[v]=tot++;
}
int bfs(int s,int t)
{
    mem(vis,0);
    mem(dis,0);
    queue<int>q;
    q.push(s);
    vis[s]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=first[u]; ~i; i=e[i].next)
        {
            if(!vis[e[i].v]&&e[i].w>0)
            {
                vis[e[i].v]=1;
                dis[e[i].v]=dis[u]+1;
                q.push(e[i].v);
            }
        }
    }
    return dis[t];
}
int dfs(int u,int t,int flow)
{
    if(u==t)return flow;
    for(int i=first[u]; ~i; i=e[i].next)
    {
        if(dis[e[i].v]==dis[u]+1&&e[i].w>0)
        {
            int dd=dfs(e[i].v,t,min(e[i].w,flow));
            if(dd)
            {
                e[i].w-=dd;
                e[i^1].w+=dd;
                return dd;
            }
        }
    }
    dis[u]=0;
    return 0;
}
int Dinic(int s,int t)
{
    int ans=0,flow;
    while(bfs(s,t))
    {
        while(flow=dfs(s,t,INF))
            ans+=flow;
    }
    return ans;
}
void init()
{
    mem(first,-1);
    tot=0;
}
char e1[105][105],e2[105][105];
int n,m,d,t,kase=0;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        init();
        int ans=0;
        scanf("%d%d",&n,&d);
        for(int i=1; i<=n; i++)
            scanf("%s",e1[i]+1);
        m=strlen(e1[1]+1);
        for(int i=1; i<=n; i++)
        {
            scanf("%s",e2[i]+1);
            for(int j=1; j<=m; j++)
            {
                int u=(i-1)*m+j;
                if(e1[i][j]>'0')
                {
                    add_edge(u,n*m+u,e1[i][j]-'0');

                    if(i<=d||j<=d||i+d>n||j+d>m)
                    {
                        add_edge(u+n*m,2*n*m+1,INF);
                    }
                    else
                    {
                        for(int x=-d; x<=d; x++)
                        {
                            for(int y=-d; y<=d; y++)
                            {
                                if((x||y)&&abs(x)+abs(y)<=d)
                                {
                                    int tx=i+x;
                                    int ty=j+y;
                                    if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&e1[tx][ty]>'0')
                                    {
                                        int v=(tx-1)*m+ty;
                                        add_edge(n*m+u,v,INF);
                                    }
                                }
                            }
                        }
                    }
                    }
                    if(e2[i][j]=='L')
                    {
                        ans++;
                        add_edge(0,u,1);
                    }
                }
            }int now=ans-Dinic(0,2*n*m+1);
            if(now==0) printf("Case #%d: no lizard was left behind.\n",++kase);
            else if(now==1) printf("Case #%d: 1 lizard was left behind.\n",++kase);
            else printf("Case #%d: %d lizards were left behind.\n",++kase,now);
        }


        return 0;
    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值