BZOJ1066: [SCOI2007]蜥蜴

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1066

化点权为边权。s->L,c=1;每个有权值的点拆成两个点,相连边权为a[i][j],然后距离<=d的点再相连。注意跳出去的判断。。

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define inf int(1e9)
using namespace std;
struct data{int from,obj,pre,c;
}e[500500];
int d;
char ch[100];
int head[4040],a[21][21],uu[4040],cur[4040],n,m,tot,sum,t;
int read(){
    int x=0,f=1; char ch=getchar();
    while (!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while (isdigit(ch)) {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
int p(int x,int y){
    return (x-1)*m+y;
}
void insert(int x,int y,int z){
    e[++tot].obj=y; e[tot].from=x; e[tot].pre=head[x]; e[tot].c=z; head[x]=tot;
    e[++tot].obj=x; e[tot].from=y; e[tot].pre=head[y]; e[tot].c=0; head[y]=tot;
}
int sqr(int x){
    return x*x;
}
int dis(int x,int y,int xx,int yy){
    return sqr(x-xx)+sqr(y-yy);
}
bool jud(int x,int y){
    rep(i,1,m) if (dis(x,y,0,i)<=d*d) return 1;
    rep(i,1,m) if (dis(x,y,n+1,i)<=d*d) return 1;
    rep(i,1,n) if (dis(x,y,i,0)<=d*d) return 1;
    rep(i,1,n) if (dis(x,y,i,m+1)<=d*d) return 1;  
    return 0;
}
bool bfs(){
    queue<int> q; q.push(0); clr(uu,-1); uu[0]=0;
    while (!q.empty()){
        int u=q.front(); q.pop(); 
        for (int j=head[u];j;j=e[j].pre){
            int v=e[j].obj;
            if (uu[v]==-1&&e[j].c){
                uu[v]=uu[u]+1;  
                q.push(v);
            }
        }
    }
    if (uu[t]==-1) return 0;
    return 1;
}
int dfs(int x,int mx){
    if (x==t) return mx;
    int used=0;
    for (int j=cur[x]; j;j=e[j].pre){
        int v=e[j].obj;
        if (uu[v]==uu[x]+1){
            int w=dfs(v,min(e[j].c,mx-used));
            e[j].c-=w; e[j^1].c+=w; used+=w;
            if (e[j].c) cur[x]=j;
            if (used==mx) return mx;
        }
    }
    if (!used) uu[x]=-1;
    return used;
}
int dinic(){
    int ans=0;
    while (bfs()){
        rep(i,0,t) cur[i]=head[i];
        ans+=dfs(0,inf);
    }
    return ans;
}
int main(){
    n=read(); m=read(); d=read();
    rep(i,1,n){
        scanf("%s",ch+1);
        rep(j,1,m) a[i][j]=ch[j]-'0';
    }
    tot=1; t=2*n*m+1;
    rep(i,1,n) {
        scanf("%s",ch+1);
        rep(j,1,m) if (ch[j]=='L') {sum++,insert(0,p(i,j),1);}
    }
    rep(i,1,n) rep(j,1,m) if (a[i][j]){
        insert(p(i,j),p(i,j)+n*m,a[i][j]);
    }
    rep(i,1,n) rep(j,1,m) if (a[i][j]){
        rep(k,i-d,i+d) rep(o,j-d,j+d) if (a[k][o]&&(i!=k||j!=o)&&dis(i,j,k,o)<=d*d) insert(p(i,j)+n*m,p(k,o),inf);
        if (jud(i,j)) insert(p(i,j)+n*m,t,inf);
    }
    printf("%d\n",sum-dinic());
    return 0;
}

 

posted on 2015-12-11 12:29  ctlchild  阅读(149)  评论(0编辑  收藏  举报

导航