[BZOJ1066] [SCOI2007]蜥蜴

题目描述

Description

  在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃
到边界外。 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平面距离不超过d的任何一个石
柱上。石柱都不稳定,每次当蜥蜴跳跃时,所离开的石柱高度减1(如果仍然落在地图内部,则到达的石柱高度不
变),如果该石柱原来高度为1,则蜥蜴离开后消失。以后其他蜥蜴不能落脚。任何时刻不能有两只蜥蜴在同一个
石柱上。

Input

  输入第一行为三个整数r,c,d,即地图的规模与最大跳跃距离。以下r行为石竹的初始状态,0表示没有石柱
,1~3表示石柱的初始高度。以下r行为蜥蜴位置,“L”表示蜥蜴,“.”表示没有蜥蜴。

Output

  输出仅一行,包含一个整数,即无法逃离的蜥蜴总数的最小值。

Sample Input

5 8 2
00000000
02000000
00321100
02000000
00000000
........
........
..LLLL..
........
........

Sample Output

1

HINT

100%的数据满足:1<=r, c<=20, 1<=d<=4

题目分析

最大流 恶心的建图方式= =
根据题目的含义,石柱的高度就是在这个石柱上能够通过的蜥蜴数量。
因此我们要考虑如何限制通过的蜥蜴数量。非常显然的,我们将一个石柱拆成两个点,相当于即将落地和即将起跳的过程,中间连一条流量为石柱高度的边,就能做到限制通过的蜥蜴数。随后从源到有蜥蜴的点连一条流量为1的边,代表这个点有一条蜥蜴。
对于两两间曼哈顿距离小于d的石柱a和b,我们在这两个石柱拆出来的点的每一对即将起跳和即将落地的点上连一条双向的边,流量为INF,代表从石柱a跳到石柱b,或从石柱b跳到石柱a。如果石柱高度为,那么即使蜥蜴跳过去了也并没有什么用 = = 所以这样连边是成立的。
最后对于到边界距离小于d的石柱,连一条边到汇,代表跳出地图,这样最大流就等于最多跳出地图的蜥蜴数。
最后用蜥蜴总数相减即可。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
int n,m,ans;
int tot=1,head[1010*4],to[1010*1010*3],net[1010*1010*3],val[1010*1010*3];
int s,t;
void add(int x,int y,int c)
{
    net[++tot]=head[x],head[x]=tot,to[tot]=y,val[tot]=c;
    net[++tot]=head[y],head[y]=tot,to[tot]=x,val[tot]=0;
}
int dis[1010*4];
int bfs()
{
    queue<int>q;
    memset(dis,0,sizeof dis);
    q.push(s);
    dis[s]=1;
    while(q.size())
    {
        int nmp=q.front();
        q.pop();
        for(int i=head[nmp];i;i=net[i])
            if(val[i]>0&&!dis[to[i]])
            {
                dis[to[i]]=dis[nmp]+1;
                q.push(to[i]);
                if(to[i]==t) return 1;
            }
    }
    return 0;
}
int dinic(int x,int flow)
{
    int tmp,temp=flow;
    if(x==t) return flow;
    for(int i=head[x];i;i=net[i])
        if(val[i]>0&&dis[to[i]]==dis[x]+1)
        {
            tmp=dinic(to[i],min(val[i],temp));
            if(tmp==0) dis[to[i]]=0;
            temp-=tmp,val[i]-=tmp,val[i^1]+=tmp;
            if(!temp) break;
        }
    return flow-temp;
}
int r,c,d;
char sa[100];
int mp[50][50];
int sqr(int x){
    return x*x;
}
double dist(int x1,int y1,int x2,int y2){
    return sqrt(sqr(x1-x2)+sqr(y1-y2));
}
int pos(int x,int y,int add){
    return (x-1)*c+y+r*c*add;
}
bool can(int x,int y){
    return x<=d||r-x<d||y<=d||c-y<d;
}
int main()
{
    int num=0;
    scanf("%d%d%d",&r,&c,&d);
    s=0;t=2*r*c+1;
    for(int i=1;i<=r;i++)
    {
        scanf("%s",sa+1);
        for(int j=1;j<=c;j++) mp[i][j]=sa[j]-'0';
    }
    for(int i=1;i<=r;i++)
    {
        scanf("%s",sa+1);
        for(int j=1;j<=c;j++)
            if(sa[j]=='L') num++,add(s,pos(i,j,0),1);
    }
    for(int i=1;i<=r;i++)
        for(int j=1;j<=c;j++)
            if(mp[i][j])
                add(pos(i,j,0),pos(i,j,1),mp[i][j]);
    for(int x1=1;x1<=r;x1++)
        for(int y1=1;y1<=c;y1++)
            if(mp[x1][y1]) 
                for(int x2=1;x2<=r;x2++)
                    for(int y2=1;y2<=c;y2++)
                    {
                        if(x1==x2&&y1==y2) continue;
                        if(mp[x2][y2]&&dist(x1,y1,x2,y2)<=d)
                            add(pos(x1,y1,1),pos(x2,y2,0),1<<30),add(pos(x2,y2,1),pos(x1,y1,0),1<<30);

                    }
    for(int i=1;i<=r;i++)
        for(int j=1;j<=c;j++)
            if(can(i,j)) add(pos(i,j,1),t,1<<30);
    while(bfs()) ans+=dinic(s,1<<30);
    printf("%d",num-ans);
}

发表评论

邮箱地址不会被公开。 必填项已用*标注