码迷,mamicode.com
首页 > Windows程序 > 详细

HDU 2732 Leapin' Lizards (最大流)

时间:2016-06-27 19:38:18      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:

Leapin‘ Lizards

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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.
 
题意:有一些蜥蜴在图中的柱子上,用L表示,每次能跳d个距离到另一个柱子上,跳出边缘就是安全的了。每个柱子只能跳有限次,之后会崩塌,问你最后不能跳出的最少蜥蜴的个数
分析:柱子是有限制的,因此可以将它拆点拆开,两点的容量为能跳的次数,对于能直接跳出边缘的柱子,将它和汇点相连,对于每个蜥蜴,将它和能跳到的柱子相连,跑最大流就行了。
//============================================================================
// Name        :
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
//****************************************************
//最大流模板  SAP算法
//邻接表形式
//******************************************************
const int MAXN=2200;
const int MAXE=200020;
const int INF=0x3f3f3f3f;
struct Node
{
    int to,next,cap;
}edge[MAXE];
int tol;
int head[MAXN];
int gap[MAXN],dis[MAXN],pre[MAXN],cur[MAXN];

void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int rw=0)
{
    edge[tol].to=v;edge[tol].cap=w;edge[tol].next=head[u];head[u]=tol++;
    edge[tol].to=u;edge[tol].cap=rw;edge[tol].next=head[v];head[v]=tol++;
}
int SAP(int start,int end,int nodenum)
{
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    memcpy(cur,head,sizeof(head));
    int u=pre[start]=start,maxflow=0,aug=-1;
    gap[0]=nodenum;
    while(dis[start]<nodenum)
    {
        loop:
        for(int &i=cur[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap&&dis[u]==dis[v]+1)
            {
                if(aug==-1||aug>edge[i].cap)
                    aug=edge[i].cap;
                pre[v]=u;
                u=v;
                if(v==end)
                {
                    maxflow+=aug;
                    for(u=pre[u];v!=start;v=u,u=pre[u])
                    {
                        edge[cur[u]].cap-=aug;
                        edge[cur[u]^1].cap+=aug;
                    }
                    aug=-1;
                }
                goto loop;
            }
        }
        int mindis=nodenum;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap&&mindis>dis[v])
            {
                cur[u]=i;
                mindis=dis[v];
            }
        }
        if((--gap[dis[u]])==0)break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];
    }
    return maxflow;
}

char m1[30][30];
char m2[30][30];
int mat[30][30];

int main()
{
    int T;
    int n,d;
    int iCase=0;
    scanf("%d",&T);
    while(T--){
        iCase++;
        scanf("%d%d",&n,&d);
        init();
        int tol=0;
        for(int i=0;i<n;i++)
            scanf("%s",m1[i]);
        int m=strlen(m1[0]);
        memset(mat,0,sizeof(mat));
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(m1[i][j]>0){
                    mat[i][j]=++tol;
                    addedge(2*tol-1,2*tol,m1[i][j]-0);
                }
            }
        }
        int st=0,ed=2*tol+1,nodenum=2*tol+2;
        int sum=0;
        for(int i=0;i<n;i++){
            scanf("%s",m2[i]);
            for(int j=0;j<m;j++){
                if(m2[i][j]==L){
                    sum++;
                    addedge(st,2*mat[i][j]-1,1);
                }
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(mat[i][j]){
                    for(int x=-d;x<=d;x++){
                        for(int y=abs(x)-d;y<=d-abs(x);y++){
                            int mx=i+x;
                            int my=j+y;
                            if(mx<0||mx>=n||my<0||my>=m) continue;
                            if(mat[mx][my]==0) continue;
                            if(mx==i&&my==j) continue;
                            addedge(2*mat[i][j],2*mat[mx][my]-1,INF);
                        }
                    }
                    if(i<d||j<d||n-i<=d||m-j<=d)
                        addedge(2*mat[i][j],ed,INF);
                }
            }
        }
        int ans=sum-SAP(st,ed,nodenum);
        if(ans==0) printf("Case #%d: no lizard was left behind.\n",iCase);
        else if(ans==1) printf("Case #%d: 1 lizard was left behind.\n",iCase);
        else printf("Case #%d: %d lizards were left behind.\n",iCase,ans);
    }
    return 0;
}

 

HDU 2732 Leapin' Lizards (最大流)

标签:

原文地址:http://www.cnblogs.com/wangdongkai/p/5620719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!