"BZOJ2127" Happiness

Source: Internet
Author: User

Tag: pos color instantiation Inpu + = Code Tput main indicates

Time Limit: ms Memory Limit: 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.

Input

The first row of two positive integers n,m. Next is the six matrix the first matrix is n rows m column the number of row J of this matrix is the value of the students ' choice of the Humanities in column J of row I. The second matrix is n rows m column the number in column J of this matrix indicates the joy value that the classmate of the seat in column J of Row I has chosen for science. The third matrix is n-1 row m column the number in column J of this matrix indicates the additional joy value obtained by the students in row J of section I and the students of section J of Row I+1. The fourth matrix is a n-1 row m column the number in column J of this matrix indicates the additional joy value obtained by the students in column J of row I and the students of section J of Row I+1. The fifth matrix is n rows m-1 columns column I of row J of this matrix indicates that the students in column J of row I, and the students of row I of section j+1, choose the additional joy value obtained by the Liberal Arts. The sixth matrix is n rows m-1 columns column I of row J of this matrix indicates the additional joy value obtained by the students of the seat in column J of row I and the classmates in row I of section j+1.

Output

Outputs an integer that represents the maximum value of the sum of joy values

Sample Input1 2
1 1
100 110
1
1000
Sample Output1210
"Sample description"
When both men are chosen, they get the 100+110+1000 of joy.
"Data Size"
For data within 100%, n,m<=100 all Joy values are non-negative integers less than or equal to 5000
Solution

The problem of this adjacent lattice is generally considered as the minimum cut model of two adjacent squares, and then all the models are superimposed.

Now consider the two adjacent $x$ and $y$. The proceeds of the $x $ election or rationale are $x_0,x_1$; $y the proceeds of the election or the rationale are $y_0,y_1$; the income of the selected text is $s_0$, and the benefit of the election is $s_1$.

Then the minimum cut from total revenue $sum=x_0+x_1+y_0+y_1+s_0+s_1$ is the optimal solution.

Consider the logical relationship:

(1) If $x$ and $y$ are selected, they need to be cut $x_1,y_1,s_1$

(2) If $x$ and $y$ are chosen, then they need to be cut $x_0,y_0,s_0$

(3) If $x$ Wen $y$, then need to cut $x_1,y_0,s_0,s_1$

(4) If $x$ $y$ the text, then need to cut $x_0,y_1,s_0,s_1$

Find a person, a family, must cut off another branch of joy value. Then from the source point $s$ to the edge of the $x$ $x_0$, to $y$ connected to the edge of $y_0$, $x $ to the meeting point $t$ the edge of $x_1$, to $y$ connected to the edge of $y_1$.

  

Now, (1) and (2) are also poor $s_0$ and $s_1$ not portrayed. Because after cutting off two side looks like the picture is completely separated, not good again joins the new side to embody, we consider the $s_0$ attach in $x_0$ and $y_0$, will $s_1$ attach in $x_1$ and $y_1$, namely each cent half:

   

(1) and (2) finished. However (3) and (4) are not used in it.

If the $x$ election, $y $ selection, then cut off the edge is right and bottom left two edge, the sum of its weights is $x_1+\frac{1}{2}s_1+y_0+\frac{1}{2}s_0$, but we expect is $x_1+y_0+s_0+s_1$, not ah.

What to do? Expectations and current values are $\frac{1}{2}s_0+\frac{1}{2}s_1=\frac{1}{2} (s_0+s_1) $. Think of ways to instantiate it! When the cut is the upper right and bottom left two side, has formed the smallest cut, then forcibly put this frontier fortress in! $y$ to $x$ with a weighted value of $\frac{1}{2} (S_0+s_1) $ of the adjustment edge.

$x $ $y$ in the same vein.

  

The two adjustment sides in the middle, only play a role when the two arts are different. Now this model has been able to characterize (1) ~ (4) all the situation.

Model the whole image, run the minimum cut, use the total revenue and subtract the minimum cut.

Important:

We can't connect $ (s,x), (S,y), (x,t), (y,t) $, total revenue, and see the sum of the $sum$ of every two squares for every two squares, and then run the model up and up.

Why? Because of this we will find some $x_0$, $x _1$ and $y_0$, $y _1$ was more than a few times, this is very bad, the model between the intersection.

in fact, for a point $x$, we combine all the sides of the $s$-> $x $: The weight should be: $x _0$ or $y_0$ plus $\frac{1}{2} \sum s_0$ . $x $ to $t$ 's side.

And the middle adjustment side as usual.

So when the $sum$ for all of the income of the lattice, and the $sum$ minus the smallest cut the answer is right.

Tips:

You can omit decimals by multiplying the Benquan by 2, running out the smallest cut and dividing by 2.

#include <cstdio>#include<queue>using namespacestd;Const intn=10010, inf=2147000000;intn,m,a[ the][ the],b[ the][ the],ax[ the][ the],ay[ the][ the],bx[ the][ the],by[ the][ the];intsum;intS,t,dis[n],cur[n],h[n],tot;queue<int>Q;structedge{intV,next,f;} g[n* -];inlineintIdintXintY) {return(X-1) *m+y;} InlineintMinintXintY) {returnX<y?x:y;} InlinevoidAddedge (intUintVintf) {g[++tot].v=v; G[tot].f=f; G[tot].next=h[u]; h[u]=tot; g[++tot].v=u; g[tot].f=0; G[TOT].NEXT=H[V]; h[v]=tot;}BOOLBFs () { while(!q.empty ()) Q.pop ();    Q.push (S);  for(intI=1; i<=t;i++) dis[i]=-1; Dis[s]=0;  while(!Q.empty ()) {        intu=Q.front (); Q.pop ();  for(intI=h[u],v;i;i=g[i].next)if(g[i].f&&dis[v=g[i].v]==-1) {Dis[v]=dis[u]+1; if(v==t)return true;            Q.push (v); }    }    returndis[t]!=-1;}intDfsintUintDelta) {    if(u==t)returnDelta; intret=0,Get;  for(intI=cur[u],v;i&&delta;i=g[i].next)if(g[i].f&&dis[v=g[i].v]==dis[u]+1){            Get=Dfs (V,min (DELTA,G[I].F)); G[I].F-=Get; G[i^1].f+=Get; if(G[I].F) cur[u]=i; Delta-=Get; RET+=Get; }    if(!ret) dis[u]=-1; returnret;}intDinic () {intret=0;  while(BFS ()) { for(intI=1; i<=t;i++) cur[i]=H[i]; RET+=DFS (S,inf); }    returnret;}intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) scanf ("%d", &a[i][j]), sum+=A[i][j];  for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) scanf ("%d", &b[i][j]), sum+=B[i][j];  for(intI=1; i<n;i++) for(intj=1; j<=m;j++) scanf ("%d", &ax[i][j]), sum+=Ax[i][j];  for(intI=1; i<n;i++) for(intj=1; j<=m;j++) scanf ("%d", &bx[i][j]), sum+=Bx[i][j];  for(intI=1; i<=n;i++) for(intj=1; j<m;j++) scanf ("%d", &ay[i][j]), sum+=Ay[i][j];  for(intI=1; i<=n;i++) for(intj=1; j<m;j++) scanf ("%d", &by[i][j]), sum+=By[i][j]; S=n*m+1; t=n*m+2; tot=1;  for(intI=1; i<=n;i++)         for(intj=1; j<=m;j++){            intu=ID (i,j); Addedge (S,u,a[i][j]*2+ax[i-1][j]+ax[i][j]+ay[i][j-1]+Ay[i][j]); Addedge (U,t,b[i][j]*2+bx[i-1][j]+bx[i][j]+by[i][j-1]+By[i][j]); if(i<N) {Addedge (U,id (i+1, j), ax[i][j]+Bx[i][j]); Addedge (ID (i+1, j), u,ax[i][j]+Bx[i][j]); }            if(j<m) {Addedge (U,id (i,j+1), ay[i][j]+By[i][j]); Addedge (ID (i,j+1), u,ay[i][j]+By[i][j]); }        }    int Get=Dinic (); Get/=2; printf ("%d\n", sum-Get); return 0;}
Magic Code

"BZOJ2127" Happiness

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.