hdu2732 最大流~

Leapin' Lizards

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 20   Accepted Submission(s) : 13
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.
 
关键要理解图 和会见图~~一般的网络流都是对边作为限制,此题是点,可以拆掉分析 对于哪些不能跳出去但是又有柱子的点,那么我们就去按照跳跃距离搜寻有没有其他的柱子能够去跳跃,如果能够找到的话,那么连接这两点,并且将容量控制为弧尾节点的柱子数,也正是由于一条弧只能够约束一个顶点,所以我们需要进行拆点,点内之间流量为本身柱子数。。注意英语语法~单复数
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char num[50][50];
char L[50][50];
struct node
{
    int u,v,w;
};
node e[5555555];
int first[10000];
int next[5555555];
int cc;
int d,n,l,t;
int sum=0;
void add_edge(int u,int v,int f1)
{
    e[cc].u=u;
    e[cc].v=v;
    e[cc].w=f1;
    next[cc]=first[u];
    first[u]=cc;
    cc++;

    e[cc].u=v;
    e[cc].v=u;
    e[cc].w=0;
    next[cc]=first[v];
    first[v]=cc;
    cc++;
}
int sap(int s,int t)
{
    int gap[10000],dis[10000],curedge[10000],pre[10000];
    int cur_flow,flow_ans=0,u,tmp,neck,i;
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    memset(pre,-1,sizeof(pre));
    for(i=0;i<=t;i++)
        curedge[i]=first[i];
    //gap[0]=
    u=s;
    while(dis[0]<t+1)
    {
        if(u==t)
        {
            cur_flow=99999999;
            for(i=s;i!=t;i=e[curedge[i]].v)
            {
                if(cur_flow>e[curedge[i]].w)
                {
                    neck=i;
                    cur_flow=e[curedge[i]].w;
                }
                //printf("%d\n",i);
                //rintf("f1gas\n");
            }
            for(i=s;i!=t;i=e[curedge[i]].v)
            {
                tmp=curedge[i];
                e[tmp].w-=cur_flow;
                e[tmp^1].w+=cur_flow;
                //printf("fgasfj\n");
            }
            flow_ans+=cur_flow;
            u=neck;
        }
        for(i=curedge[u];i!=-1;i=next[i])
        {
            if(e[i].w&&(dis[u]==dis[e[i].v]+1))
                break;
        }
        if(i!=-1)
        {
            curedge[u]=i;
            pre[e[i].v]=u;
            u=e[i].v;
        }
        else
        {
            //if(0==--gap[dis[u]])
                //break;
            curedge[u]=first[u];
            for(tmp=t+1,i=first[u];i!=-1;i=next[i])
            {
                if(e[i].w)
                {
                    tmp=min(tmp,dis[e[i].v]);
                }
            }
            dis[u]=tmp+1;
            //++gap[dis[u]];
            if(u!=s)
                u=pre[u];
        }
        //printf("fgas\n");
    }
    return flow_ans;
}
int abs(int a)
{
    return a>0?a:-a;
}
void build_map()
{

    int i,j;
    for(i=0;i<n*l;i++)
    {
        int y=i%l;
        int x=i/l;
        if(num[x][y]!='0')
        {
            add_edge(i+1,i+1+n*l,num[x][y]-'0');
        }
    }
    for(i=0;i<n*l;i++)
    {
        int y=i%l+1;
        int x=i/l+1;
        if((x<=d)||(y<=d)||(n+1-x<=d)||(l+1-y<=d))
        {
            if(num[x-1][y-1]!='0')
                add_edge(i+1+n*l,t,num[x-1][y-1]-'0');
        }
    }

    for(i=0;i<n*l;i++)
    {
        for(j=i+1;j<n*l;j++)
        {
            int y1=i%l;
            int x1=i/l;
            if(num[x1][y1]!='0')
            {
                int y2=j%l;
                int x2=j/l;
                if(abs(y2-y1)+abs(x2-x1)<=d)
                {
                    if(num[x2][y2]!='0')
                    {
                        add_edge(i+1+n*l,j+1,num[x1][y1]-'0');
                        add_edge(j+1+n*l,i+1,num[x1][y1]-'0');
                    }

                }
            }
        }
    }
    /*for(i=1;i<=n;i++)
    {
        for(j=1;j<=l;j++)
        {
            int k;
            if(num[i][j]!='0')
            {
                for(k=1;k<=d;k++)
                {
                    if(i+k<=n&&num[i+k][j]!='0')
                    {
                        add_edge((i-1)*l+j,(i+k-1)*l+j,num[i][j]-'0',num[i+k][j]-'0');
                       // printf("%d %d\n",(i-1)*l+j,(i+k-1)*l+j);
                    }
                    if(j+k<=l&&num[i][j+k]!='0')
                    {

                        add_edge((i-1)*l+j,(i-1)*l+j+k,num[i][j]-'0',num[i][j+k]-'0');
                        //printf("%d %d\n",(i-1)*l+j,(i-1)*l+j+k);
                    }
                }
            }
        }
    }*/


    for(i=0;i<l*n;i++)
    {
        int y=i%l;
        int x=i/l;
        if(L[x][y]=='L')
        {
            add_edge(0,i+1,1);
            sum++;
        }
    }
}

int main()
{
    int T,cas;
    scanf("%d",&T);
    for(cas=1;cas<=T;cas++)
    {

        scanf("%d%d",&n,&d);
        int i;
        memset(first,-1,sizeof(first));
        memset(next,-1,sizeof(next));
        memset(num,0,sizeof(num));
        memset(L,0,sizeof(0));
        sum=0;
        for(i=0;i<n;i++)
            scanf("%s",num[i]);
        for(i=0;i<n;i++)
            scanf("%s",L[i]);
        l=strlen(num[0]);
        t=n*l*2+1;
        cc=0;
        build_map();
        //printf("%d",sum);
        /*for(i=0;i<=t;i++)
        {
            int j;
            printf("%d:",i);
            for(j=first[i];j!=-1;j=next[j])
            {
                printf(" %d-%d",e[j].v,e[j].w);
            }
            printf("\n");
        }*/
        int j;
        int res=sum-sap(0,t);
        if(res>1)
            printf("Case #%d: %d lizards were left behind.\n",cas,res);
        else if(res)
            printf("Case #%d: %d lizard was left behind.\n",cas,res);
        else
            printf("Case #%d: no lizard was left behind.\n",cas);

    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值