BZOJ1066: [SCOI2007]蜥蜴

来源:互联网 发布:网络用语小白什么意思 编辑:程序博客网 时间:2024/05/20 06:27

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的边
最后把每个互相到达的石墩连起来,因为每只蜥蜴一开始都在石墩上,所以可行
再跑一遍网络流,把蜥蜴总数减去能出去的条数即可

代码如下:

#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;struct node{    int x,y,c,next,other;} a[2100000];int len,last[2100000];void ins(int x,int y,int c){    int k1,k2;    k1=++len;    a[len].x=x;    a[len].y=y;    a[len].c=c;    a[len].next=last[x];    last[x]=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 head,tail;int list[2100000];int st,ed,h[2100000];bool bt_h(){    memset(h,0,sizeof(h));    h[st]=1;    list[1]=st;    head=1;    tail=2;    while(head!=tail)    {        int x=list[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;                list[tail++]=y;            }        }        head++;    }    if(h[ed]>0)return true;    return false;}int findflow(int x,int f){    if(x==ed)return f;    int s=0,t=0;    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&&s<f)        {            s+=(t=findflow(y,min(a[k].c,f-s)));            a[k].c-=t;            a[a[k].other].c+=t;        }    }    if(s==0)h[x]=0;    return s;}int n,m,d;char c[31];int f[31][31];int main(){    scanf("%d%d%d",&n,&m,&d);    len=0;    memset(last,0,sizeof(last));    int mn=m*n,x;    st=2*mn;ed=st+1;    char c[25];    for(int i=0;i<n;i++)    {        scanf("%s",c);        for(int j=0;j<m;j++)        {            f[i][j]=c[j]-'0';            if(f[i][j]==0)continue;            if(i<d||j<d||i+d>=n||j+d>=m)ins(i*m+j+mn,ed,9999999);            ins(i*m+j,i*m+j+mn,f[i][j]);        }    }    int sum=0;    for(int i=0;i<n;i++)    {        scanf("%s",c);        for(int j=0;j<m;j++)            if(c[j]=='L')            {                ins(st,i*m+j,1);                sum++;            }    }    for(int i=0;i<n;i++)        for(int j=0;j<m;j++)        {            if(f[i][j]==0)continue;            for(int x=max(0,i-d);x<=min(n-1,i+d);x++)            {                for(int y=max(0,j-d);y<=min(m-1,j+d);y++)                {                    if(x==i&&y==j)continue;                    if(f[x][y]==0)continue;                    if(abs(x-i)+abs(y-j)>d)continue;                    ins(i*m+j+mn,x*m+y,999999);                }            }        }    int ans=0;    while(bt_h())ans+=findflow(st,999999);    printf("%d\n",sum-ans);    return 0;}

by_lmy

原创粉丝点击