Description
Input
The first line is a three positive integer p,q,r, representing the cut cake's long p, wide Q, and high R. The second row has a nonnegative integer d, which indicates smoothness requirements. Next is the matrix of R P row Q columns, the x row of the z matrix is V (x,y,z) (1≤x≤p, 1≤y≤q, 1≤z≤r).
100% of the data satisfies the p,q,r≤40,0≤d≤r, and all the disharmony values given are not more than 1000. Output
Contains only an integer that represents the smallest total disharmony value on a legal basis. Sample Input
2 2 2
1
6 1
6 1
2 6
2 6 Sample Output
6 HINT
The best slice F is f (1,1) =f (2,1) =2,f (1,2) =f (2,2) =1 The idea of solving problems:
Consider the minimum cut.
First of all, it must be (i,j,k) to (i,j,k+1) the edge of the F (i,j,k).
How to satisfy a condition that is less than D. Which is to cut (i,j,k) This side, a side of (I ', J ', k-d~k+d) must be cut off, so (i,j,k) to (i ', J ', k-d), (i ', J ', k+d+1) to the side of the INF (i ', J ', k+1).
#include <bits/stdc++.h> using namespace std;
int Getint () {int I=0,f=1;char C; For (C=getchar ();(c!= '-') && (c< ' 0 ' | |
C> ' 9 '); C=getchar ());
if (c== '-') F=-1,c=getchar ();
for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) i= (i<<3) + (i<<1) +c-' 0 ';
return i*f;
const int n=100005,m=1000005,inf=0x3f3f3f3f;
const int fx[4]={-1,0,1,0};
const int fy[4]={0,-1,0,1};
int p,q,r,d,s,t,n,p[45][45][45];
int tot=1,dis[n],cur[n],first[n],nxt[m],to[m],cap[m];
queue<int>q;
void Add (int x,int y,int z) {nxt[++tot]=first[x],first[x]=tot,to[tot]=y,cap[tot]=z;
nxt[++tot]=first[y],first[y]=tot,to[tot]=x,cap[tot]=0;
BOOL BFs () {while (!q.empty ()) Q.pop ();
for (int i=s;i<=t;i++) dis[i]=-1,cur[i]=first[i];
Q.push (S), dis[s]=0;
while (!q.empty ()) {int U=q.front (); Q.pop ();
for (int e=first[u];e;e=nxt[e]) {int v=to[e]; if (Dis[v]==-1&&cap[e]) {Dis[v]=dis[U]+1,q.push (v);
if (v==t) return true;
}} return false;
int dinic (int u,int flow) {if (u==t) return flow;
int res=0;
for (int &e=cur[u];e;e=nxt[e]) {int v=to[e];
if (Dis[v]==dis[u]+1&&cap[e]) {int det=dinic (v,min (flow-res,cap[e)));
Cap[e]-=det,cap[e^1]+=det;
Res+=det;if (Res==flow) break;
} if (Res<flow) dis[u]=-1;
return res;
int main () {//freopen ("lx.in", "R", stdin);
P=getint (), Q=getint (), R=getint (), D=getint ();
for (int i=1;i<=p;i++) for (int j=1;j<=q;j++) for (int k=1;k<=r;k++) p[i][j][k]=++n;
S=0,t=++n;
for (int k=1;k<=r;k++) for (int i=1;i<=p;i++) for (int j=1;j<=q;j++) {
int Z=getint ();
if (k==1) Add (S,p[i][j][k],inf);
K==r?add (p[i][j][k],t,z): Add (p[i][j][k],p[i][j][k+1],z); for (iNT l=0;l<4;l++) {int di=i+fx[l],dj=j+fy[l]; if (di<1| | di>p| | dj<1| |
DJ>Q) continue;
K>d?add (P[i][j][k],p[di][dj][k-d],inf): Add (P[i][j][k],p[di][dj][1],inf);
} int ans=0;
while (BFS ()) ans+=dinic (S,inf);
cout<<ans;
return 0; }