BZOJ3144: [Hnoi2013]切糕

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

把每一条z轴都拿出来,s->(x,y,1),c=f[x][y][1];(x,y,k)->(x,y,k+1),c=f[x][y][k];(x,y,r)->t,c=inf

然后对于四联通的点,(x,y,z)->(x',y’,z-d)

似乎这叫经典的最小割模型?

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#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 maxn 100050
#define ll long long
#define inf int(1e9)
#define eps 1e-6
using namespace std;
struct data{int obj,pre,c;
}e[1000500];
int n,tot=1,m,r,t,d,f[45][45][45],uu[maxn];
int head[maxn],cur[maxn],dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
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,int z){
    if (z==0) return 0;
    return (z-1)*n*m+(x-1)*m+y;
}
void insert(int x,int y,int z){
    e[++tot].obj=y; e[tot].pre=head[x]; e[tot].c=z; head[x]=tot;
    e[++tot].obj=x; e[tot].pre=head[y]; e[tot].c=0; head[y]=tot;
}
bool jud(int x,int y){
    if (x<1||y<1||x>n||y>m) return 0;
    return 1;
}
void build(){
    rep(i,1,n) rep(j,1,m){
        rep(k,1,r) insert(p(i,j,k-1),p(i,j,k),f[i][j][k]); insert(p(i,j,r),t,inf);
        rep(k,0,3){
            int x=i+dx[k],y=j+dy[k];
            if (jud(x,y)) rep(o,d+1,r) insert(p(i,j,o),p(x,y,o-d),inf);
        }
    }   
}
bool bfs(){
    queue<int >q;
    clr(uu,-1); q.push(0); 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));
            used+=w; e[j].c-=w; e[j^1].c+=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(); r=read(); t=n*m*r+1;
    d=read();
    rep(i,1,r)
        rep(j,1,n) 
            rep(k,1,m) f[j][k][i]=read();
    build();
    printf("%d\n",dinic()); 
    return 0;
}

 

posted on 2015-12-04 18:10  ctlchild  阅读(139)  评论(0编辑  收藏  举报

导航