The topic asks us to choose as many points as possible, while 22 is not adjacent
You can think of the chess board in accordance with the chessboard pattern dyeing, the same color of the point is definitely not adjacent to each other, at the same time we also convert the graph to a two-part map
The title requirement becomes the maximum point weight independent set in the binary chart.
Maximum independent set: Contains the set of as many vertices as possible, where any two points are not adjacent, so-called non-adjacent, that is, two points without a connecting edge
Minimum point overlay: Select the minimum number of points to associate the points with all edges (overwrite all edges)
Maximum point weight independent set = total weight-Minimum point weight cover set
It's not hard to understand, just look at the definition.
Assuming that the minimum point coverage has now been obtained, the points are removed and the remaining points are definitely not adjacent, meaning that the remaining points can form a separate set
And the coverage we're seeking is minimal, so the independent set that we get naturally is the largest.
The problem is converted to the minimum point weight coverage set
The minimum point coverage and the minimum point weight coverage set of the two graphs can be solved by network flow.
Composition:
The super source point is connected with each point of the left set, if the minimum point coverage is obtained, the weight value is 1, and if the minimum point weight is set, the weighted value is the point right of the point.
The super meeting point is connected to each point on the right.
And then add the original edge of the binary graph to the graph, the weight value is infinity
#include"Cstdio"#include"Queue"#include"Cmath"#include"Stack"#include"iostream"#include"algorithm"#include"CString"#include"Queue"#include"Map"#include"Set"#include"Vector"#definell Long Long#defineMEMS (A, B) memset (A,b,sizeof (a))#defineLS pos<<1#defineRS Pos<<1|1using namespacestd;Const intMAXN = -;Const intMaxe =40500;Const intINF =99999999;structnode{intS,e,next,cost,val; Node () {} node (intAintBintCintD): s (a), E (b), Next (c), Val (d) {}}edge[maxe];intTot,ans,n;intFIRST[MAXN],DEP[MAXN],GAP[MAXN];intmat[ -][ -];intdx[4]={0,0,-1,1};intdy[4]={1,-1,0,0};voidinit () {tot=0; MEMS (First,-1);}voidAddedge (intUintVintW) { //cout<<u<< ' \ t ' <<v<< ' \ t ' <<w<<endl;edge[tot]=node (u,v,first[u],w); First[u]=tot++; Edge[tot]=node (V,u,first[v],0); FIRST[V]=tot++;}intSapintSrcintdes) {memset (DEP,0,sizeof(DEP)); Memset (Gap,0,sizeof(GAP)); gap[0]=des+1; inttop=0, ans=0, u=Src,i; intCur[maxn],s[maxe]; //s[top++]=src; for(i=0; i<=des;i++) Cur[i]=First[i]; while(1){ if(u==des) { intminv=Inf,pos; for(i=0; i<top;i++){ if(minv>edge[s[i]].val) {MINV=Edge[s[i]].val; POS=i; } } for(i=0; i<top;i++) {Edge[s[i]].val-=MINV; Edge[s[i]^1].val+=MINV; } ans+=MINV; Top=POS; U=Edge[s[pos]].s; } for(i=cur[u];i!=-1; i=edge[i].next)if(edge[i].val&&dep[u]==dep[edge[i].e]+1) Break; if(i!=-1) {Cur[u]=i; S[top++]=i; U=EDGE[I].E; } Else{ if(--gap[dep[u]]==0) Break; intmindep=des+1; for(i=first[u];i!=-1; i=Edge[i].next) { if(!edge[i].val)Continue; if(mindep>DEP[EDGE[I].E]) {MINDEP=DEP[EDGE[I].E]; Cur[u]=i; }} Dep[u]=mindep+1; Gap[dep[u]]++; if(U!=SRC) u=edge[s[--Top]]. S } } returnans;}BOOLingintXinty) { if(x>=0&&x<n&&y>=0&&y<n)return true; return false;}intMain () {//freopen ("In.txt", "R", stdin); while(~SCANF ("%d",&N)) {init (); inttot=0; for(intI=0; i<n;i++) for(intj=0; j<n;j++) {scanf ("%d",&Mat[i][j]); Tot+=Mat[i][j]; } intSrc=0; intdes=n*n+1; for(intI=0; i<n;i++) for(intj=0; j<n;j++){ if((i+j)%2==0) Addedge (src,i*n+j+1, Mat[i][j]); ElseAddedge (i*n+j+1, Des,mat[i][j]); } for(intI=0; i<n;i++) for(intj=0; j<n;j++) if((i+j)%2==0) for(intk=0;k<4; k++){ intx=i+Dx[k]; inty=j+Dy[k]; if(!ing (x, y))Continue; Addedge (i*n+j+1, x*n+y+1, INF); } printf ("%d\n", tot-SAP (Src,des)); } return 0;}
View Code
HDU 1565: Maximum point weight independent set (network flow)