[Bzoj2127]happiness

Source: Internet
Author: User

Face poke Me

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 six matrices the first matrix is n rows m column
The number in column J of line I of this matrix indicates the joy value that the students of the seat in column J of row I have chosen for the humanities.
The second matrix is n rows m columns
The number in column J of line I of this matrix indicates the joy value that the students of the seat in column J of row I have chosen for science.
The third matrix is a n-1 row m column
The number in column J of row I of this matrix indicates the additional joy value obtained by the students at the same time in column J of row I, and the students of section I+1, who chose the humanities.
The fourth matrix is n-1 row m column
The number in column J of row I of this matrix indicates the additional joy value obtained by the students at the same time as the students in column J of row I, and the classmates of section i+1.
The fifth matrix is n rows m-1 columns
The numbers in column J of line I of this matrix indicate that the students in row J of line I are also selected for the additional Joy value obtained by the humanities at the same time as the students in row j+1 of section I.
The sixth matrix is n rows m-1 columns
The number in column J of row I of this matrix indicates the additional joy value obtained by the students at the same time in row I of row J of the Class I and the classmate of line I of section j+1.

Output

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

Sample Input

1 2
1 1
100 110
1
1000

Sample Output

1210

"Sample description"

When both men are chosen, they get the 100+110+1000 of joy.

"Data Size"

For 100% of data, n,m<=100 all Joy values are non-negative integers less than or equal to 5000

Sol

First, the maximum benefit is converted to the minimum loss and the minimum cut model is established. Add each number in each matrix first, and then use the minimum cut to solve the minimum loss.
Below, we use a two-tuple\ ((p,q) \)To represent the network from the\ (p\)Connected to\ (q\)The capacity of the edge.
We have selected the liberal arts in\ (s\)On this side, the selection of science\ (t\)This side. So to a single point\ (x\)The process is simple: direct order\ ((s,x) =\)The proceeds of the liberal arts,\ ((x,t) =\)You can choose the benefits of science.
But how do you calculate the profit (or loss) of the two people at the same time in the selection or selection?
We're thinking about two people.\ (x\),\ (y\), the benefit of both of them in the election is\ (u\), the benefit of a selection is\ (v\)。 We have a potential\ (2^2=4\)discussed separately in different situations.
1.\ (x\),\ (y\)All selected text. The side that is cut off at this time is\ ((x,t) \)And\ ((y,t) \), the total loss should be\ (v\);
2.\ (x\),\ (y\)All chosen. The side that is cut off at this time is\ ((s,x) \)And\ ((s,y) \), the total loss should be\ (u\);
3.\ (x\)Selected text\ (y\)The election manager. The side that is cut off at this time is\ ((x,t) \)、\ ((s,y) \)And\ ((x, y) \), the total loss should be\ (u+v\);
4.\ (x\)Choose a Reason\ (y\)Selected text. The side that is cut off at this time is\ ((s,x) \)、\ ((y,t) \)And\ ((y,x) \), the total loss should be\ (u+v\)。
From the above we can get a set of equations:

\ ((x,t) + (y,t) =v\)
\ ((s,x) + (s,y) =u\)
\ ((x,t) + (s,y) + (x, y) =u+v\)
\ ((s,x) + (y,t) + (y,x) =u+v\)

(I'm too vegetable to write the markdown of the blog Park ... Can only be written in formula form)
As long as the Junior high school mathematics graduation know that this equation must not be solved (indefinite equation), but considering that this thing does not affect the other, so we can arbitrarily bring a set of feasible solution in.
Make:

$ (x,t) = V/2 $
$ (y,t) = V/2 $
$ (s,x) = U/2 $
$ (s,y) = U/2 $
$ (x, y) = (U + V)/2 $
$ (y,x) = (U + V)/2 $

According to the above-mentioned method can be built.
Since dividing by two may result in floating point numbers, consider enlarging all sides by twice times to ensure that all capacity is a positive integer.

Code
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespaceStdConst intINF =1e9;Const intN = the;structedge{intTo,next,w;} a[n*n<<3];intn,m,p[n][n],tot,v[6][n][n],s,t,head[n*n],cnt=1,dep[n*n],cur[n*n],sum;queue<int>Q;intGI () {intx=0, w=1;CharCh=getchar (); while((ch<' 0 '|| Ch>' 9 ') &&ch!='-') Ch=getchar ();if(ch=='-') w=0, Ch=getchar (); while(ch>=' 0 '&&ch<=' 9 ') x= (x<<3) + (x<<1) +ch-' 0 ', Ch=getchar ();returnW?x:-x;}voidLinkintUintVintWBOOLb) {a[++cnt]= (edge) {v,head[u],w};    head[u]=cnt; A[++cnt]= (Edge) {u,head[v],b?w:0}; head[v]=cnt;}BOOLBFS () {memset (DEP,0,sizeof(DEP)); dep[s]=1; Q.push (S); while(! Q.empty ()) {intU=q.front (); Q.pop (); for(intE=head[u];e;e=a[e].next)if(A[e].w&&!dep[a[e].to]) Dep[a[e].to]=dep[u]+1, Q.push (a[e].to); }returnDEP[T];}intDfsintUintFlow) {if(u==t)returnFlow for(int&e=cur[u];e;e=a[e].next)if(A[e].w&&dep[a[e].to]==dep[u]+1)        {intTemp=dfs (A[e].to,min (A[e].w,flow));if(temp) {a[e].w-=temp;a[e^1].w+=temp;returntemp;} }return 0;}intDinic () {intres=0; while(BFS ()) { for(inti=t;i;i--) Cur[i]=head[i]; while(intTemp=dfs (S,inf)) res+=temp; }returnRes;}intMain () {N=gi (); M=gi (); S=n*m+1; T=n*m+2; for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) P[i][j]=++tot; for(intk=0;k<6; k++) {intX=n,y=m;if(k==2|| k==3) x--;if(k==4|| k==5) y--; for(intI=1; i<=x;i++) for(intj=1; j<=y;j++) V[k][i][j]=gi (), sum+=v[k][i][j]; } for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) {link (s,p[i][j], (v[0][i][j]<<1) +v[2][i][j]+v[2][i-1][j]+v[4][i][j]+v[4][i][j-1],0); Link (p[i][j],t, (v[1][i][j]<<1) +v[3][i][j]+v[3][i-1][j]+v[5][i][j]+v[5][i][j-1],0);if(i<n) Link (p[i][j],p[i+1][j],v[2][i][j]+v[3][I][J],1);if(j<m) Link (p[i][j],p[i][j+1],v[4][i][j]+v[5][I][J],1); } printf ("%d\n", sum-(Dinic () >>1));return 0;}

[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.