Description
The language is not good, read a long time to read the question.
The idea behind the simplification is probably this: given a p*q grid, the cost of putting a number Z (1<=z<=r) in (X,y) is V (x,y,z), and the absolute difference between the number of squares in the four adjacent lattices cannot be greater than D.
Ask for the minimum price.
P,q,r≤40,0≤d≤r Solution
If you read the original question, it should be easy to think of the smallest cut.
Because after the simplification the topic was confused.
In other words, we're going to divide the cut cake into two sets.
A simple idea is that each point to its next layer points to a side right for its weight value.
The point of the first layer is connected to the source point.
The last layer of the point is connected to the point of infinity.
How to guarantee restrictions.
All we need is a positive infinity edge to the point at which the number of layers adjacent to it is Difference D.
Draw the picture to know that the correctness is obvious ... Code
#include <cstdio> #include <cstring> #include <algorithm> #define FO (i,a,b) for (int i=a;i<=b;i++) #
Define REP (i,a) for (int i=last[a];i;i=next[i]) #define N 64505 using namespace std;
const int INF=0X7FFFFFFF;
int Dis[n],d[n],d,s,t,l,all,p,q,r,x,ans;
int last[n],next[n*12],t[n*12],f[n*12],g[4][2]={0,1,1,0,0,-1,-1,0}; int get (int x,int y,int z) {return (x-1) *p*q+ (y-1) *q+z;} void Add (int x,int y,int z) {t[++l]=y;f[l]=z;next[l]=l
Ast[x];last[x]=l;
T[++l]=x;f[l]=0;next[l]=last[y];last[y]=l;
BOOL BFs () {memset (dis,0,sizeof (dis));
int i=0,j=1;d[1]=s;dis[s]=1;
while (I<J) rep (K,d[++i]) if (!dis[t[k]]&&f[k)) {dis[t[k]]=dis[d[i]]+1;
D[++J]=T[K];
return dis[t];
int dinic (int x,int y) {if (x==t) return y;
int now=0;
Rep (i,x) if (f[i]&&dis[t[i]]==dis[x]+1) {int k=dinic (t[i],min (f[i],y));
F[i]-=k;f[i^1]+=k;y-=k;now+=k;
if (!y) break; } if(!now) Dis[x]=-1;
return to now; int main () {scanf ("%d%d%d%d", &p,&q,&r,&d); all=p*q*r; s=0;
t=all+1;l=1;
Fo (i,1,r) fo (j,1,p) fo (k,1,q) {scanf ("%d", &x);
if (i==1) Add (S,get (i,j,k), x);
else Add (Get (I-1,j,k), get (I,j,k), x);
if (i>d) fo (s,0,3) {int x=j+g[s][0],y=k+g[s][1]; if (x<1| | x>p| | y<1| |
Y>Q) continue;
Add (I,j,k), get (i-d,x,y), INF);
} if (i==r) Add (Get (i,j,k), t,inf);
while (BFS ()) ans+=dinic (S,inf);
printf ("%d", ans); }