Description Science and Arts Division is a very tangled matter! (Although people who see this topic certainly have not tangled up) small p in the class to carry out the arts and Sciences Division. His class can be described by a n*m matrix, each of which represents a classmate's seat. Each student must choose a subject from the arts and sciences. Students will get a satisfactory value when they choose the subject. The satisfaction value is obtained as follows: 1. If the classmate of line I second J chooses the liberal arts, he will get art[i][j] satisfaction value, if choose Science, will get SCIENCE[I][J] satisfaction value. 2. If the students in row J of line I chose the liberal arts, and he was next to each other (two squares and only if they had an identical edge), all the students chose the liberal arts, he would be happier, so he would increase same_art[i][j] satisfaction. 3. If the students in row J of Line I chose science, and all of his classmates chose science, the satisfaction value of same_science[i]j[] was increased. Small p wants to know how people should choose, in order to make all people's satisfaction value and maximum. Please tell him the maximum value.
Input first behavior two positive integers: N,m next n technique m Integer, representing Art[i][j]; next n the number of M integers. SCIENCE[I][J]; next n-M integer representing SAME_ART[I][J];
Output is an integer that represents the sum of the maximum satisfactory values
Sample Input3 4
13 2 4 13
7 13 8 12
18 17 0 5
8 13 15 4
11 3 8 11
11 18 6 5
1 2 3 4
4 2 3 2
3 1 0 4
3 2 3 2
0 2 2 1
0 2 4 4Sample Output theHINT
Sample Description
1 The choice of liberal arts, 0 for the selection of science, the programme is as follows:
1 0 0 1
0 1 0 0
1 0 0 0
N,M<=100, read into the data are <=500 this incomplete topic description is SMG. First add the proceeds, and then try to minimize the modeling. We set the node of S-cut to indicate the selected text, and the node of T-cut represents the selection reason. So for each point x, a volume from S to X is the edge of art, from X to t a capacity of science is attached. It is not difficult to find that if you want to meet a same_art condition for x, X and the node adjacent to x cannot belong to T-cut, so you can create a new node Q, from S to Q with a capacity of Same_art edge, and from Q to X and the node adjacent to X to connect a capacity of the INF edge. Same_science conditions are the same.
#include <cstdio>#include<cctype>#include<queue>#include<cmath>#include<cstring>#include<algorithm>#defineRep (i,s,t) for (int i=s;i<=t;i++)#defineDwn (i,s,t) for (int i=s;i>=t;i--)#defineren for (int i=first[x];i!=-1;i=next[i])using namespacestd;Const intBuffersize=1<< -;Charbuffer[buffersize],*head,*Tail;inlineCharGetchar () {if(head==tail) { intL=fread (Buffer,1, Buffersize,stdin); Tail= (Head=buffer) +l; } return*head++;} InlineintRead () {intx=0, f=1;CharC=Getchar (); for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1; for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;}Const intmaxn=30010;Const intmaxm=1000010;Const intinf=1e9;structisap{structtedge{intX,y,w,next;} ADJ[MAXM];intMS,FCH[MAXN]; intD[maxn],s[maxn],cur[maxn],gap[maxn],n,top; voidInitintN) { This->n=n;ms=0; top=0; memset (d,-1,sizeof(d)); memset (FCH,-1,sizeof(FCH)); return; } voidAddedge (intUintVintW) {Adj[ms]= (Tedge) {u,v,w,fch[u]};fch[u]=ms++; ADJ[MS]= (Tedge) {v,u,0, fch[v]};fch[v]=ms++; return; } voidBFs () {Queue<int>Q; Q.push (n);d [n]=0; while(!Q.empty ()) { intu=Q.front (); Q.pop (); for(inti=fch[u];i!=-1; i=Adj[i].next) { intv=adj[i].y; if(d[v]==-1) d[v]=d[u]+1, Q.push (v); } } return; } intSolveintSintT) {N=t;bfs ();intk=s,i,flow=0; for(i=0; i<=n;i++) cur[i]=fch[i],gap[d[i]]++; while(d[s]<N) { if(k==N) { intMi=Inf,pos; for(i=0; i<top;i++)if(ADJ[S[I]].W<MI) mi=adj[s[i]].w,pos=i; for(i=0; i<top;i++) adj[s[i]].w-=mi,adj[s[i]^1].w+=mi; Flow+=mi;top=pos;k=adj[s[top]].x; } for(i=cur[k];i!=-1; i=Adj[i].next) { intv=adj[i].y; if(adj[i].w&&d[k]==d[v]+1) {cur[k]=i;k=v;s[top++]=i; Break;} } if(i==-1){ intlim=N; for(i=fch[k];i!=-1; i=Adj[i].next) { intv=adj[i].y; if(Adj[i].w&&d[v]<lim) lim=d[v],cur[k]=i; } if(--gap[d[k]]==0) Break; D[K]=lim+1; gap[d[k]]++; if(k!=s) k=adj[s[--Top]]. X } } returnflow; }}sol;intn,m,sum,v;intIdintXintYintT) {returnt*n*m+ (x1) *m+y;}Const intmx[]={0,1,-1,0,0};Const intmy[]={0,0,0,1,-1};intMain () {n=read (), m=read (); ints=n*m*3+1, t=n*m*3+2; Sol.init (T); Rep (I,1, N) Rep (J,1, m) Sol. Addedge (S,id (I,j,0), V=read ()), sum+=v; Rep (I,1, N) Rep (J,1, m) Sol. Addedge (ID (i,j,0), T,v=read ()), sum+=v; Rep (I,1, N) Rep (J,1, M) {Sol. Addedge (S,id (I,j,1), V=read ()); sum+=v; Rep (dir,0,4) { intnx=i+mx[dir],ny=j+My[dir]; if(nx>=1&&nx<=n&&ny>=1&&ny<=m) Sol. Addedge (ID (i,j,1), ID (Nx,ny,0), INF); }} rep (I,1, N) Rep (J,1, M) {Sol. Addedge (ID (i,j,2), T,v=read ()); sum+=v; Rep (dir,0,4) { intnx=i+mx[dir],ny=j+My[dir]; if(nx>=1&&nx<=n&&ny>=1&&ny<=m) Sol. Addedge (ID (nx,ny,0), ID (i,j,2), INF); }} printf ("%d\n", sum-sol.solve (s,t)); return 0;}
View Code
BZOJ3894: Science and Arts Division