hdu 2732(最大流+拆点)

Leapin' Lizards

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


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.
 

解题思路:这道题是网络流问题,题目意思比较难懂,就是每次最多条d步,并且要落在柱子上,而且每个柱子只能跳特定的次数。。最开始我考虑建图的时候,一直在想如果说A,B两点都是可以相互可跳到的,那么A->B,和B->A肯定是要建两次的,那不就有环了嘛。。。。后面参考了别人的思路,确实是我想多了,有环又有什么关系呢??因为每条边的流量都是被限定的,就算有环也不可能改变整个流网络的最大流,而且定义里面也没有说不能有环。。。然后就是拆点的问题,因为你每个柱子只能够通过一定的次数,多了就不行,也类似于顶点有流量限制,由于普通的网络流只是限制了边,所以每个顶点分成两个相同的点,两点之间建立一条容量为顶点通过次数的有向边。。。
这道题确实是好题,解决了我对网络流的一些模糊的地方。。。

AC:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;

const int MAXN=2200;
const int MAXM=200020;
const int INF=0x3f3f3f3f;
struct Node
{
    int to,next,cap;
}edge[MAXM];
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 g1[30][30];
char g2[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",&g1[i]);
        int m=strlen(g1[0]);
        memset(mat,0,sizeof(mat));
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(g1[i][j]>'0')
                {
                    mat[i][j]=++tol;
                    addedge(2*tol-1,2*tol,g1[i][j]-'0');
                }

        int start=0,end=2*tol+1,nodenum=2*tol+2;
        //进行拆点,加上源点和汇点,共2*tol+2个点。
        int sum=0;//总数
        for(int i=0;i<n;i++)
        {
            scanf("%s",&g2[i]);
            for(int j=0;j<m;j++)
                if(g2[i][j]=='L')
                {
                    sum++;
                    addedge(start,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 newi=i+x;
                            int newj=j+y;
                            if(newi<0||newi>=n||newj<0||newj>=m)continue;
                            if(mat[newi][newj]==0)continue;
                            if(newi==i&&newj==j)continue;
                            addedge(2*mat[i][j],2*mat[newi][newj]-1,INF);
                        }
                    if(i<d||j<d||n-i<=d||m-j<=d)
                        addedge(2*mat[i][j],end,INF);
                }
        int ans=sum-sap(start,end,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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值