bzoj3894: Science and Arts Division

Source: Internet
Author: User

Test instructions: To a n*m grid map, each point is dyed black there is a benefit, dyeing White has a profit, if the point adjacent (there is a public side) of the lattice with it color, there will be an additional benefit, to seek the maximum benefit scheme.

Consider the idea of a similar maximum closed sub-graph, we will calculate the total revenue first, and then run the minimum cut, and finally the total proceeds to reduce the minimum cut is the answer.

How to build a map? We have to ensure that a point of the liberal arts, it around the selection of science can not have the science of extra income, choose Science also the same.

Consider this: Each point is split into 3 points, the first point of the split to the point surrounding it to the INF, while the point itself to the first point of the split of the Arts additional value, the second point is the opposite of the first point, that is, the point connected to the first point (adjacent points) to it with the INF, The second point is then removed to the body to connect the science extra value. Each point body, s to its even liberal arts plus the arts Extra value, which to the T-even science plus science extra value.

When you think about it, you will find that if a traitor is found in the surrounding area, the extra value that is counted will be deducted.

At first, I thought of the smallest cut to do, but I always can not solve how to remove the extra value (because the thought is to split only one point), again%ihopenot.

1#include <bits/stdc++.h>2 using namespacestd;3 #defineN 2000054 #defineINF 1e95 #defineID (x, y) (((1) *m+ (y))6InlineintRead () {7     intx=0, f=1;CharA=GetChar ();8      while(a<'0'|| A>'9') {if(a=='-') f=-1; A=GetChar ();}9      while(a>='0'&& a<='9') x=x*Ten+a-'0', a=GetChar ();Ten     returnx*F; One } A Const intdir[4][2]={1,0,-1,0,0,1,0,-1}; - ints,t,p,n,m,cur[n],head[n],ans,cnt,d[n],si[ the][ the],ai[ the][ the],alls[ the][ the],alla[ the][ the]; - BOOLVis[n]; thequeue<int>Q; - structedges{ -     intTo,cap,flow,next; -}e[2*N]; +InlinevoidInsertintUintVintc) { -e[cnt]= (edges) {v,c,0, head[u]};head[u]=cnt++; +e[cnt]= (edges) {u,0,0, head[v]};head[v]=cnt++; A } atInlineBOOLBFs () { -memset (Vis,0,sizeof(Vis)); -d[s]=0; Q.push (S); vis[s]=1; -      while(!Q.empty ()) { -         intx=Q.front (); Q.pop (); -          for(inti=head[x];i>=0; i=e[i].next) in             if(!vis[e[i].to] && e[i].cap>E[i].flow) -d[e[i].to]=d[x]+1, vis[e[i].to]=1, Q.push (e[i].to); to     } +     returnVis[t]; - } the intDfsintXinta) { *     if(x==t | |!a)returnA; $     intf,flow=0;Panax Notoginseng      for(int& i=cur[x];i>=0; i=E[i].next) { -         if(d[e[i].to]==d[x]+1&& (F=dfs (E[i].to,min (a,e[i].cap-e[i].flow)) >0) thee[i].flow+=f,flow+=f,e[i^1].flow-=f,a-=F; +         if(!a) Break; A     } the     returnflow; + } -InlineintMaxflow () { $     intflow=0; $      while(BFS ()) { -          for(inti=s;i<=t;i++) cur[i]=Head[i]; -flow+=DFS (s,inf); the     } -     returnflow;Wuyi } the intMain () { -memset (head,-1,sizeof(head)); WuN=read (); M=read (); s=0; t=3*n*m+1; p=n*m; -      for(intI=1; i<=n;i++) About          for(intj=1; j<=m;j++) $Ai[i][j]=read (), ans+=Ai[i][j]; -      for(intI=1; i<=n;i++) -          for(intj=1; j<=m;j++) -Si[i][j]=read (), ans+=Si[i][j]; A      for(intI=1; i<=n;i++) +          for(intj=1; j<=m;j++) theAlla[i][j]=read (), ans+=Alla[i][j]; -      for(intI=1; i<=n;i++) $          for(intj=1; j<=m;j++) theAlls[i][j]=read (), ans+=Alls[i][j]; the      for(intI=1; i<=n;i++) the          for(intj=1; j<=m;j++){ theInsert (S,id (i,j), ai[i][j]+alla[i][j]); -Insert (ID (i,j), t,si[i][j]+alls[i][j]); inInsert (ID (I,J), ID (i,j) +p,alla[i][j]); theInsert (ID (I,J) +2*P,id (I,J), alls[i][j]); the              for(intx,y,k=0;k<4; k++){ Aboutx=i+dir[k][0],y=j+dir[k][1]; the                 if(x<1|| X>n | | y<1|| Y&GT;M)Continue; theInsert (ID (I,J) +p,id (x, y), INF); theInsert (ID (x, y), id (i,j) +2*p,inf); +             } -         } theprintf"%d\n", ans-Maxflow ());Bayi     return 0; the}

bzoj3894: Science and Arts Division

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.