Description
A class of the seat table is a n*m matrix, after a semester to get along, each classmate and left and right adjacent classmates to each other become good friends. This semester will be a penny of science, each classmate for the choice of arts and science have their own joy value, and a pair of good friends if you can choose arts or science, then they will harvest some joy value. As a computer competition coach of the SCP Big Boss, want to know how to allocate can make the whole class of joy value the largest sum.
Solution
It's the same way as the liberal arts division. 1 A
#include <iostream>#include<cstdlib>#include<cstdio>#include<cstring>#include<queue>#defineINF 0x3f3f3f3fusing namespacestd;intn,m,s,t,ans=0;intlevel[50005],head[50005],cnt=0;structnode{intNext,to,cap;} edges[500005];intRead () {intx=0, f=1;CharC=GetChar (); while(c<'0'|| C>'9'){ if(c=='-') f=-1; c=GetChar (); } while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar (); } returnx*F;}voidAddedge (intUintVintc) {Edges[cnt].next=Head[u]; Head[u]=CNT; Edges[cnt].to=v; Edges[cnt++].cap=C;}voidInsertintUintVintc) {Addedge (u,v,c); Addedge (V,u,0);}intPosintXintY) {return(X-1) *m+y;} Queue<int>Q;BOOLBFs () {memset (level,-1,sizeof(level)); Q.push (s); Level[s]=0; while(!Q.empty ()) { intu=Q.front (); Q.pop (); for(intI=head[u];~i;i=Edges[i].next) { intv=edges[i].to; if(level[v]==-1&&edges[i].cap) Level[v]=level[u]+1, Q.push (v); } } if(level[t]==-1)return false; return true;}intDfsintUintf) { if(u==t)returnF; intflow=0, D; for(intI=head[u];~i&&flow<f;i=Edges[i].next) { intv=edges[i].to; if(level[v]==level[u]+1&&Edges[i].cap) {D=dfs (V,min (fflow,edges[i].cap)); Flow+=D; Edges[i].cap-=D; Edges[i^1].cap+=D; } } if(!flow) level[u]=-1; returnflow;}intMain () {memset (head,-1,sizeof(head)); N=read (), m=read (); S=0, t=n*m+1; for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) { intx=read (); Insert (S,pos (I,J), x); Ans+=x; } for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) { intx=read (); Insert (POS (i,j), t,x); Ans+=x; } intnum=t+1; for(intI=1; i<=n-1; i++) for(intj=1; j<=m;j++) { intx=read (); Insert (S,NUM,X); Insert (Num,pos (i,j), INF); Insert (Num,pos (i+1, J), INF); Ans+=x; Num++; } for(intI=1; i<=n-1; i++) for(intj=1; j<=m;j++) { intx=read (); Insert (NUM,T,X); Insert (POS (i,j), num,inf); Insert (POS (i+1, J), Num,inf); Ans+=x; Num++; } for(intI=1; i<=n;i++) for(intj=1; j<=m-1; j + +) { intx=read (); Insert (S,NUM,X); Insert (Num,pos (i,j), INF); Insert (Num,pos (i,j+1), INF); Ans+=x; Num++; } for(intI=1; i<=n;i++) for(intj=1; j<=m-1; j + +) { intx=read (); Insert (NUM,T,X); Insert (POS (i,j), num,inf); Insert (POS (i,j+1), Num,inf); Ans+=x; Num++; } intD; while(BFS ()) { while(d=DFS (S,inf)) ans-=D; } printf ("%d\n", ans); return 0;}
[Bzoj 2127]happiness (min cut)