• BZOJ1189: [HNOI2007]紧急疏散evacuate


    【传送门:BZOJ1189


    简要题意:

      给出一个字符矩阵,'D'表示出口(不止一个出口),'.'表示空地,'X'表示墙(也就是不可走),保证墙和出口都在地图边缘。一开始每个空地上都有一个人,每个人可以上下左右四个方向行走,也可以停下来不走,每秒可以走一格,而每一块空地上可以同时站无数个人,但是在出口处,每秒只能站一个人,而其他人就要等待一秒钟才能到出口,求出每个人到出口的最短时间(PS:如果一个人到第i个出口的路径上有其它出口的话,那么这个人就不能通过这条路径到达第i个出口,但这个人可以到达这条路径上的其它出口)

    特殊样例:

    输入:

    4 4
    DXXD
    X..D
    X..X
    DXXX

    输出:

    4


    题解:

      首先将每个空地(相当于每个人)与每个出口的最短路径求出来,然后用二分答案,网络流判断来得出答案

      网络流建边:将起始点与每个人的编号(自己定义)相连,流量为1,然后一开始我就傻傻地想着直接把人与出口直接连在一起,结果发现,两个人如果同时在出口的附近的话,那么其中一个人就要等1秒,也就是等另外一个人从出口出去,这样就会导致网络流出错。所以我们就把每个出口拆点,每个出口都拆成x个点(x代表当前二分到的答案),用来表示出口的第1秒钟到第x秒钟的状态,这样就能避免同一时间多人进入出口了,流量也是1。最后把出口拆成的所有点与结束点相连,流量也设置为1。


    参考代码:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cstdlib>
    #include<cmath>
    using namespace std;
    char st[21][21];
    int dx[5]={0,1,-1,0,0};
    int dy[5]={0,0,0,1,-1};
    struct zuobiao
    {
        int x,y;
    }dr[410],man[410],list[410];
    int dlen,mlen;
    int dr1[21][21];
    int man1[21][21];
    int head,tail;
    bool v[21][21];
    int d[21][21];
    int jl[410][410];
    int n,m;
    struct node
    {
        int x,y,c,next,other;
    }a[2110000];int len,last[1110000];
    void ins(int x,int y,int c)
    {
        int k1,k2;
        len++;k1=len;
        a[len].x=x;a[len].y=y;a[len].c=c;
        a[len].next=last[x];last[x]=len;
        len++;k2=len;
        a[len].x=y;a[len].y=x;a[len].c=0;
        a[len].next=last[y];last[y]=len;
        a[k1].other=k2;
        a[k2].other=k1;
    }
    int blist[1110000],h[1110000];
    int stt,ed;
    bool bt_h()
    {
        memset(h,0,sizeof(h));h[stt]=1;
        blist[1]=stt;head=1;tail=2;
        while(head!=tail)
        {
            int x=blist[head];
            for(int k=last[x];k;k=a[k].next)
            {
                int y=a[k].y;
                if(h[y]==0&&a[k].c>0)
                {
                    h[y]=h[x]+1;
                    blist[tail++]=y;
                }
            }
            head++;
        }
        if(h[ed]>0) return true;
        else return false;
    }
    int findflow(int x,int f)
    {
        if(x==ed) return f;
        int s=0,t;
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(h[y]==(h[x]+1)&&a[k].c>0&&f>s)
            {
                t=findflow(y,min(a[k].c,f-s));
                s+=t;
                a[k].c-=t;a[a[k].other].c+=t;
            }
        }
        if(s==0) h[x]=0;
        return s;
    }
    void bfs(int k)
    {
        zuobiao start=man[k];
        memset(v,false,sizeof(v));v[start.x][start.y]=true;
        for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) d[i][j]=999999999;
        d[start.x][start.y]=0;
        list[1]=start;
        head=1;tail=2;
        while(head!=tail)
        {
            zuobiao tno=list[head];
            int x=tno.x,y=tno.y;
            for(int i=1;i<=4;i++)
            {
                int tx=x+dx[i],ty=y+dy[i];
                if(tx<1||ty<1||tx>n||ty>m||st[tx][ty]=='X') continue;
                if(d[tx][ty]>d[x][y]+1)
                {
                    d[tx][ty]=d[x][y]+1;
                    if(st[tx][ty]=='D') jl[k][dr1[tx][ty]]=min(jl[k][dr1[tx][ty]],d[tx][ty]);
                    if(v[tx][ty]==false&&st[tx][ty]!='D')
                    {
                        v[tx][ty]=true;
                        list[tail].x=tx;
                        list[tail].y=ty;
                        tail++;
                    }
                }
            }
            head++;
        }
    }
    int b[410][410];
    bool check(int x)
    {
        stt=0,ed=mlen+dlen*x+1;
        len=0;memset(last,0,sizeof(last));
        for(int i=1;i<=mlen;i++) ins(stt,i,1);
        for(int i=1;i<=mlen;i++)
        {
            bool bk=false;
            for(int j=1;j<=dlen;j++)
            {
                if(jl[i][j]>x) continue;
                else
                {
                    int jll=jl[i][j];
                    while(jll<=x)
                    {
                        ins(i,(j-1)*x+mlen+jll,1);
                        jll++;
                    }
                    bk=true;
                }
            }
            if(bk==false) return false;
        }
        for(int i=mlen+1;i<=mlen+dlen*x;i++) ins(i,ed,1);
        int sum=0;
        while(bt_h()==true)
        {
            sum+=findflow(stt,999999999);
        }
        if(sum!=mlen) return false;
        return true;
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        dlen=mlen=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%s",st[i]+1);
            for(int j=1;j<=m;j++)
            {
                if(st[i][j]=='D')
                {
                    dr[++dlen].x=i;dr[dlen].y=j;
                    dr1[i][j]=dlen;
                }
                if(st[i][j]=='.')
                {
                    man[++mlen].x=i;man[mlen].y=j;
                    man1[i][j]=mlen;
                }
            }
        }
        for(int i=1;i<=mlen;i++) for(int j=1;j<=dlen;j++) jl[i][j]=999999999;
        for(int i=1;i<=mlen;i++)
        {
            bfs(i);
        }
        int l=1,r=400;
        int ans=0;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(check(mid)==true){r=mid-1;ans=mid;}
            else l=mid+1;
        }
        if(ans==0) printf("impossible
    ");
        else printf("%d
    ",ans);
        return 0;
    } 
    渺渺时空,茫茫人海,与君相遇,幸甚幸甚
  • 相关阅读:
    翻硬币
    排队打水问题
    连续邮资问题
    Linux-AWK命令
    SpringInAction第五章总结 使用配置属性
    SpringInAction 第四章笔记 保护Spring
    SpringInAction第三章笔记 --使用数据
    SpringInAction第二章笔记
    SpringInAction第一章笔记
    SpringBoot启动任务
  • 原文地址:https://www.cnblogs.com/Never-mind/p/7531114.html
Copyright © 2020-2023  润新知