Bzoj 2127 Happiness "Network Flow Dinic"

Source: Internet
Author: User

Reference: Https://www.cnblogs.com/chenyushuo/p/5144957.html has to say that this is a very good way to build a map.
Assuming that S-point selection, T-point selection, a[i][j] for (i,j) election proceeds, b[i][ J] for (I,J) election proceeds, C[i][j] for the simultaneous selection of proceeds, D[i][j] for the simultaneous selection of the proceeds.
For each point x= (i+1) *m+j, we connect
\[c[s,x]=b[i][j] \]
\[c[x,t]=a[i][j] \]
for interest-related X, y two points, connect
\[C[S,X]=D[I][J]/2 \]
\ [C[S,Y]=D[I][J]/2 \]
\[C[X,T]=C[I][J]/2 \]
\[C[Y,T]=C[I][J]/2 \]
\[C[X,Y]=C[I][J]/2+D[I][J]/2 \]
\[c[ Y,X]=C[I][J]/2+D[I][J]/2 \]
Completed diagram:

and then consider the minimum cut, the following enumeration of several cases:
is selected, that is, cut the X-selection, Y-selected Rie (x and y) are selected:

are selected, Cut out the X-selection, y selected text and (x, y) are selected text:

x selection y, that is, cut the x selection, y selection, (x, y) are selected/+ (x, y) are selected/2+ (x, y) are selected/2+ (x, y) is the choice of the text/2, that is, cut the x selection, y selection, All options, (x, y) are selected:

y selection x, that is, cut off the x selection, y selection, (x, y) are selected/+ (x, y) are selected/2+ (x, y) are selected/2+ (x, y) is the choice of the text/2, that is, cut the x selection, y selection, (x, y) are selected, (x, Y Tu Xuanwen:

For the operation divided by 2, in order to avoid the error of the next rounding, we choose to take all the traffic, and finally/2.
$ ans=sum (full benefit)-min cut $
P.S. When using the adjacency table to build a map, the first point of the menu section of the edge connected, and then to practice the same time selected revenue, otherwise it will be heavy

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespaceStdConst intn= the, e=1000005, inf=1e9, p=500005;intN,m,a[n][n],b[n][n],c[n][n],d[n][n],s,t,sum,cnt=1, H[p],le[n*n];structqwe{intNe,to,va;} E[e];intRead () {intR=0;CharP=getchar (); while(p<' 0 '|| P>' 9 ') P=getchar (); while(p>=' 0 '&&p<=' 9 ') {r=r*Ten+p-48;    P=getchar (); }returnt;;voidAddintUintVintW) {cnt++;    E[cnt].ne=h[u];    E[cnt].to=v;    E[cnt].va=w; h[u]=cnt;}BOOLBFS () {queue<int>q; Memset (LE,0,sizeof(LE)); le[s]=1; Q.push (s); while(!q.empty ()) {intU=q.front ();//cout<<u<< "Aaaaaaaaaaaa" <<endl;Q.pop (); for(intI=h[u];i;i=e[i].ne)if(E[i].va&&!le[e[i].to]) {Le[e[i].to]=le[u]+1;            Q.push (e[i].to); }    }//for (int i=0;i<=5;i++)        //cout<<le[i]<< "";    returnLE[T]; }intDfsintUintf) {//cout<<u<< "" <<f<<endl;    if(u==t) {//cout<< "!";        returnF }intUsed=0; for(intI=h[u];i;i=e[i].ne) {//cout<<u<< "" <<e[i].to<< "" <<e[i].va<<endl;;        if(e[i].va>0&&le[e[i].to]==le[u]+1)        {//cout<< "OK" <<endl;            intW=dfs (E[i].to,min (F-used,e[i].va));            E[i].va-=w; e[i^1].va+=w; Used+=w;if(used==f)returnF }    }if(!used) le[u]=-1;returnUsed;}intDinic () {intans=0; while(BFS ())) Ans+=dfs (S,inf);//,cout<<ans<<endl;    returnAns;}intMain () {N=read (), M=read (); s=0, t=n*m+1; for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) A[i][j]=read (), sum+=a[i][j]; for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) B[i][j]=read (), sum+=b[i][j]; for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) {intx= (i-1) *m+j; Add (s,x,b[i][j]<<1); Add (X,s,0); Add (x,t,a[i][j]<<1); Add (T,x,0); } for(intI=1; i<n;i++) for(intj=1; j<=m;j++) C[i][j]=read (), sum+=c[i][j]; for(intI=1; i<n;i++) for(intj=1; j<=m;j++) D[i][j]=read (), sum+=d[i][j]; for(intI=1; i<n;i++) for(intj=1; j<=m;j++) {intx= (i-1) *m+j,y=i*m+j;            Add (S,x,d[i][j]); Add (X,s,0);            Add (S,y,d[i][j]); Add (Y,s,0);            Add (X,y,c[i][j]+d[i][j]);            Add (Y,x,c[i][j]+d[i][j]);            Add (X,t,c[i][j]); Add (T,x,0);            Add (Y,t,c[i][j]); Add (T,y,0); } for(intI=1; i<=n;i++) for(intj=1; j<m;j++) C[i][j]=read (), sum+=c[i][j]; for(intI=1; i<=n;i++) for(intj=1; j<m;j++) D[i][j]=read (), sum+=d[i][j]; for(intI=1; i<=n;i++) for(intj=1; j<m;j++) {intx= (i-1) *m+j,y= (i-1) *m+j+1;            Add (S,x,d[i][j]); Add (X,s,0);            Add (S,y,d[i][j]); Add (Y,s,0);            Add (X,y,c[i][j]+d[i][j]);            Add (Y,x,c[i][j]+d[i][j]);            Add (X,t,c[i][j]); Add (T,x,0);            Add (Y,t,c[i][j]); Add (T,y,0); } printf ("%d\n", sum-(Dinic () >>1));return 0;}

Bzoj 2127 Happiness "Network Flow Dinic"

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.