bzoj3144: [Hnoi2013]切糕

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
const int maxn=45*45*45+10,maxm=300010,inf=1e9+7;
const int dx[]={1,0,-1,0,0};
const int dy[]={0,-1,0,1,0};
using namespace std;
int P,Q,R,D,pre[maxm],now[maxn],son[maxm],val[maxm],tot=1,S,T,v[45][45][45];
int enc(int a,int b,int c){return a*P*Q+b*Q+c;}
void ins(int a,int b,int c){pre[++tot]=now[a],now[a]=tot,son[tot]=b,val[tot]=c;}
void init(){
scanf("%d%d%d%d",&P,&Q,&R,&D),S=maxn-2,T=maxn-1;
for (int i=1;i<=R;i++) for (int j=1;j<=P;j++) for (int k=1;k<=Q;k++) scanf("%d",&v[i][j][k]);
for (int j=1;j<=P;j++) for (int k=1;k<=Q;k++) add(S,enc(0,j,k),inf);
for (int i=1;i<=R;i++) for (int j=1;j<=P;j++) for (int k=1;k<=Q;k++) add(enc(i-1,j,k),enc(i,j,k),v[i][j][k]);
for (int j=1;j<=P;j++) for (int k=1;k<=Q;k++) add(enc(R,j,k),T,inf);
for (int i=D;i<=R;i++) for (int j=1;j<=P;j++) for (int k=1;k<=Q;k++){
for (int t=0;t<=4;t++){
int nx=j+dx[t],ny=k+dy[t];
if (nx<1||nx>P||ny<1||ny>Q) continue;
}
}
}

bool bfs(){
memset(dis,-1,sizeof(dis));
for (int y=now[x];y;y=pre[y])
if (dis[son[y]]==-1&&val[y]){
if (++tail>maxm) tail=1;
dis[son[y]]=dis[x]+1,q[tail]=son[y];
}
}
return dis[T]>0;
}

int find(int x,int low){
if (x==T) return low;
int y,res=0;
for (y=now[x];y;y=pre[y]){
if (dis[son[y]]!=dis[x]+1||!val[y]) continue;
int tmp=find(son[y],min(low,val[y]));
res+=tmp,low-=tmp,val[y]-=tmp,val[y^1]+=tmp;
if (!low) break;
}
if (!y) dis[x]=-1;
return res;
}
void work(){while (bfs()) ans+=find(S,inf);printf("%d\n",ans);}

int main(){
init(),work();
return 0;
}