[Bzoj 2127]happiness (min cut)

Source: Internet
Author: User

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)

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.