Test instructions
In a chessboard with m*n squares, there is a positive integer in each square. Now to take the number from the square, so that any 2 number of squares without a common side, and the sum of the number of the largest. Try to design an algorithm that satisfies the requirement.
Input File Example
Input.txt
3 3
1 2 3
3 2 3
2 3 1
Output File Example
Output.txt
11
Analysis
The sum of the squares and the odd-even composition of the bipartite graph, transformed into a two-point map of the maximum independent set of the right.
Adjacent built-in edges.
Assuming that all points can be taken,
St->u flow W[u] u column and even
V->ed flow is w[v] V and is odd
U,v adjacent U->v traffic is INF indicates that if U->V adjacent, you must remove one (that is, cut off a side with the source or sink point)
is the smallest cut.
Use sum-minimum cut is the answer.
The maximum independent set of the point weights of binary graphs is a classic model.
I have not seen the two-point picture to be counted repetition, really ...
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6#include <queue>7#include <cmath>8 using namespacestd;9 #defineMAXN 1010Ten #defineINF 0XFFFFFFF One A structnode - { - intX,y,f,o,next; the}t[maxn*1010];intLen; - intFIRST[MAXN]; - - intMymin (intXintY) {returnX<y?x:y;} + intMymax (intXintY) {returnX>y?x:y;} - + voidInsintXintYintf) A { att[++len].x=x;t[len].y=y;t[len].f=F; -t[len].next=first[x];first[x]=len;t[len].o=len+1; -t[++len].x=y;t[len].y=x;t[len].f=0; -t[len].next=first[y];first[y]=len;t[len].o=len-1; - } - in intst,ed; -queue<int>Q; to intDIS[MAXN]; + BOOLBFS () - { the while(!q.empty ()) Q.pop (); *memset (dis,-1,sizeof(DIS)); $Q.push (ST);d is[st]=0;Panax Notoginseng while(!q.empty ()) - { the intx=Q.front (); + for(intI=first[x];i;i=t[i].next)if(t[i].f>0) A { the inty=t[i].y; + if(dis[y]==-1) - { $dis[y]=dis[x]+1; $ Q.push (y); - } - } the Q.pop (); - }Wuyi if(dis[ed]==-1)return 0; the return 1; - } Wu - intFfind (intXintflow) About { $ if(x==ed)returnflow; - intnow=0; - for(intI=first[x];i;i=t[i].next)if(t[i].f>0) - { A inty=t[i].y; + if(dis[y]==dis[x]+1) the { - intA=ffind (Y,mymin (flow-now,t[i].f)); $t[i].f-=A; thet[t[i].o].f+=A; thenow+=A; the } the if(Now==flow) Break; - } in if(now==0) dis[x]=-1; the returnNow ; the } About the voidoutput () the { the for(intI=1; i<=len;i+=2) +printf"%d->%d%d\n", T[I].X,T[I].Y,T[I].F); - } the Bayi intMax_flow () the { the intans=0; - while(BFS ()) - { theans+=Ffind (st,inf); the } the returnans; the } - the intbx[6]={0,0,1,0,-1}, theby[6]={0,1,0,-1,0}; the 94 intMain () the { the intn,m,sum=0; thescanf"%d%d",&n,&m);98st=n*m+1; ed=st+1; About for(intI=1; i<=n;i++) - for(intj=1; j<=m;j++)101 {102 intx,now= (I-1) *m+J;103scanf"%d",&x);104sum+=x; the if((i+j)%2==0) ins (st,now,x);106 Elseins (now,ed,x);107 }108 for(intI=1; i<=n;i++)109 for(intj=1; j<=m;j++) the {111 intnow= (I-1) *m+J; the for(intk=1; k<=4; k++)if(i+bx[k]>=1&&i+bx[k]<=n&&j+by[k]>=1&&j+by[k]<=m)113 { the intNn= (i+bx[k]-1) *m+j+By[k]; the if((i+j)%2==0) ins (now,nn,inf); the Elseins (nn,now,inf);117 }118 }119 intx=Max_flow (); -printf"%d\n", sum-x);121 return 0;122}
View Code
2016-11-04 15:32:42
"Network Flow 24 questions" No.9 check number problem (two-point graph points right maximum independent set)