1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; const int N=1e5+10,M=2e5+10,INF=1e9; const int dx[]={0,-1,0,1},dy[]={-1,0,1,0}; template<class T> inline void read(T &x) { x=0;bool f=0; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); if(f)x=~x+1; } template<class T,class ...T1> inline void read(T &x,T1 &...x1) { read(x),read(x1...); } namespace Flow { int s,t; int tot=1,head[N],ver[M],e[M],ne[M]; int d[N],now[N]; queue<int>q; inline void add_edge(int u,int v,int w) { ver[++tot]=v; e[tot]=w; ne[tot]=head[u]; head[u]=tot; } inline void add(int u,int v,int w) { add_edge(u,v,w); add_edge(v,u,0); } inline bool BFS() { while(!q.empty())q.pop(); memset(d,0,sizeof(d)); q.push(s); d[s]=1,now[s]=head[s]; while(!q.empty()) { int x=q.front(); q.pop(); for(int i=head[x];i;i=ne[i]) { int y=ver[i]; if(d[y]||!e[i])continue; d[y]=d[x]+1; now[y]=head[y]; q.push(y); if(y==t)return 1; } } return 0; } int Dinic(int x,int flow) { if(x==t)return flow; int res=flow; for(int i=now[x];i&&res;i=ne[i]) { int y=ver[i]; if(d[y]==d[x]+1&&e[i]) { int k=Dinic(y,min(res,e[i])); e[i]-=k,e[i^1]+=k; res-=k; } now[x]=i; } return flow-res; } int solve() { int maxflow=0,flow; while(BFS())while(flow=Dinic(s,INF))maxflow+=flow; return maxflow; } } int p,q,r,d; int a[50][50][50]; inline int num(int x,int y,int z) { return ((x-1)*q+y)*(r+1)+z; } int main() { read(p,q,r,d); Flow::s=num(p,q,r+1)+1,Flow::t=Flow::s+1; for(int t=1;t<=r;t++) for(int i=1;i<=p;i++) for(int j=1;j<=q;j++) read(a[t][i][j]); for(int i=1;i<=p;i++) for(int j=1;j<=q;j++) { Flow::add(Flow::s,num(i,j,1),INF); Flow::add(num(i,j,r+1),Flow::t,INF); } for(int t=1;t<=r;t++) for(int i=1;i<=p;i++) for(int j=1;j<=q;j++) Flow::add(num(i,j,t),num(i,j,t+1),a[t][i][j]); for(int t=d+1;t<=r;t++) for(int i=1;i<=p;i++) for(int j=1;j<=q;j++) for(int op=0;op<4;op++) { int ti=i+dx[op],tj=j+dy[op]; if(ti&&tj&&ti<=p&&tj<=q)Flow::add(num(i,j,t),num(ti,tj,t-d),INF); } printf("%d\n",Flow::solve()); return 0; }
|