Bzoj 2127:happiness (min cut)

Source: Internet
Author: User

Min. cut.

s even for everyone (volume: Choose the pleasure of science); everyone even t (capacity: Choose the pleasure of science).

For each group (x, Y, W) x and y add pleasure W, new node V,v x (INF), V-connected Y (INF), S even v (w)

For each group (x, Y, W) x and Y added pleasure W, new node V,x V (INF), Y-V (INF), V-connected T (W)

-------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>#include <cctype> using namespace std;const int MAXN = 60000;const int INF = 100000000;int Read () {char C = getchar ();int ret = 0;For (;!isdigit (c); c = GetChar ());For (; IsDigit (c); c = GetChar ()) ret = RET * + C-' 0 ';return ret;}int N, M, S, T, V, tot = 0;struct Edge {int to, cap;Edge *next, *rev;} e[10000000], *pt = E, *HEAD[MAXN];void Add (int u, int v, int w) {pt->to = v; pt->cap = w; pt->next = Head[u]; Head[u] = pt++;}void Addedge (int u, int v, int w) {Add (U, V, W); Add (V, u, 0);Head[u]->rev = head[v];Head[v]->rev = Head[u];}Edge *P[MAXN], *CUR[MAXN];int H[MAXN], CNT[MAXN];int Maxflow () {memset (CNT, 0, sizeof CNT);memset (h, 0, sizeof h);for (int i = 0; i < V; i++)Cur[i] = head[i];cnt[0] = V;int Flow = 0;edge* E;for (int x = S, A = INF; H[s] < V;) {For (e = cur[x]; e; e = e->next)if (e->cap && h[e->to] + 1 = = H[x]) break;if (e) {P[e->to] = cur[x] = e;a = min (A, e->cap);if ((x = e->to) = = = T) {For (; x! = S; x = p[x]->rev->to) {P[x]->cap-= A;P[x]->rev->cap + = A;}Flow + = A;A = INF;}} else {if (!--Cnt[h[x]]) break;h[x] = V;For (e = head[x]; e = e->next) if (H[e->to] + 1 < h[x] && e->cap) {h[x] = h[e->to] + 1;cur[x] = e;}cnt[h[x]]++;if (x! = S)x = p[x]->rev->to;}}return Flow;}void Init () {scanf ("%d%d", &n, &m);S = N * M; T = S + 1;for (int i = 0; i < N; i++)For (int j = 0; J < M; J + +) {int v = read ();tot + = v;Addedge (i * M + j, T, V);}for (int i = 0; i < N; i++)For (int j = 0; J < M; J + +) {int v = read ();tot + = v;Addedge (S, I * M + j, v);}V = T;for (int i = 1; i < N; i++)For (int j = 0; J < M; J + +) {int v = read ();tot + = v;Addedge (++v, T, V);Addedge (i * M + j, V, INF);Addedge ((i-1) * M + J, V, INF);}for (int i = 1; i < N; i++)For (int j = 0; J < M; J + +) {int v = read ();tot + = v;Addedge (S, ++v, V);Addedge (V, I * M + j, INF);Addedge (V, (i-1) * M + J, INF);}for (int i = 0; i < N; i++)For (int j = 1; j < M; J + +) {int v = read ();tot + = v;Addedge (++v, T, V);Addedge (i * M + j, V, INF);Addedge (i * M + j-1, V, INF);}for (int i = 0; i < N; i++)For (int j = 1; j < M; J + +) {int v = read ();tot + = v;Addedge (S, ++v, V);Addedge (V, I * M + j, INF);Addedge (V, I * M + j-1, INF);}++v;}int main () {Init ();printf ("%d\n", Tot-maxflow ());return 0;}

-------------------------------------------------------------------

2127:happiness time limit: Sec Memory Limit: 259 MB
Submit: 1119 Solved: 530
[Submit] [Status] [Discuss] 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
HINT

Source

Bzoj 2127:happiness (min cut)

Related Article

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.