思路:
xxy;
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 64005
#define INF 0x3f3f3f3f
const int dx[]={,-,,,};
const int dy[]={,,,,-};
int deep[maxn],head[maxn],E[maxn<<],V[maxn<<],F[maxn<<],ans;
int n,m,r,d,ai[][][],id[][][],s,t,cnt,que[maxn];
inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
}
inline void edge_add(int u,int v,int f)
{
E[++cnt]=head[u],V[cnt]=v,F[cnt]=f,head[u]=cnt;
E[++cnt]=head[v],V[cnt]=u,F[cnt]=,head[v]=cnt;
}
bool bfs()
{
for(int i=s;i<=t;i++) deep[i]=-;
deep[s]=,que[]=s;int h=,tail=,now;
while(h<tail)
{
now=que[h++];
for(int i=head[now];i;i=E[i])
if(F[i]&&deep[V[i]]<)
{
deep[V[i]]=deep[now]+;
if(V[i]==t) return true;
que[tail++]=V[i];
}
}
return false;
}
int flowing(int now,int flow)
{
if(now==t||flow<=) return flow;
int oldflow=,pos;
for(int i=head[now];i;i=E[i])
{
if(deep[V[i]]!=deep[now]+||!F[i]) continue;
pos=flowing(V[i],min(flow,F[i]));
F[i]-=pos,F[i^]+=pos,flow-=pos,oldflow+=pos;
if(!flow) return oldflow;
}
if(!oldflow) deep[now]=-;
return oldflow;
}
int main()
{
in(n),in(m),in(r),in(d);
for(int i=;i<=r;i++)
for(int v=;v<=n;v++)
for(int e=;e<=m;e++) in(ai[i][v][e]),id[i][v][e]=++cnt;
s=,t=cnt+,cnt=;
for(int i=;i<=n;i++)
for(int v=;v<=m;v++)
edge_add(s,id[][i][v],ai[][i][v]),edge_add(id[r][i][v],t,INF);
for(int z=;z<=r;z++)
{
for(int i=;i<=n;i++)
for(int v=;v<=m;v++) edge_add(id[z-][i][v],id[z][i][v],ai[z][i][v]);
if(z>d)
{
for(int i=;i<=n;i++)
for(int v=;v<=m;v++)
for(int e=;e<=;e++)
if(i+dx[e]>&&i+dx[e]<=n&&v+dy[e]>&&v+dy[e]<=m)
edge_add(id[z][i][v],id[z-d][i+dx[e]][v+dy[e]],INF);
}
}
while(bfs()) ans+=flowing(s,INF);
printf("%d\n",ans);
return ;
}