"bzoj2127" Happiness Network flow Minimum cut

Source: Internet
Author: User

Title 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 input

1 2 1 1 100 110 1 1000

Sample output

1210

Exercises

Minimum network flow Cut

The ontology seems to have two methods of building maps.

The first is similar to bzoj3438, which is relatively simple and easy to understand, so this konjac Konjac adopts this method.

Detailed map:

S-> students, capacity for the arts proceeds, students->t, capacity for science benefits;

S-> adjacent to the two students of the liberal arts combination (on the basis of the students added to the new point), the capacity of the liberal arts of the proceeds; adjacent to the two students of the liberal arts group, adjacent to the two students, the capacity of the INF;

Adjacent to the two students, the adjacent two students science combination, the capacity of the INF, adjacent to the two science combination->t, the capacity for the income of the selected science.

Finally, the minimum cut is obtained, and the answer is the sum of all proceeds-minimum cut.

The second kind, see http://www.cnblogs.com/chenyushuo/p/5144957.html, is about 5 times times faster than mine.

#include <cstdio> #include <cstring> #include <queue> #define N 60010 #define M 300010 #define INF 0X7FF FFFFF #define POS (i, J) (i-1) * m + j using namespace Std; Queue<int> Q; int head[n], to[m], val[m], next[m], cnt = 1, S, T, Dis[n];     void Add (int x, int y, int z) {to[++cnt] = y, val[cnt] = Z, next[cnt] = head[x], head[x] = cnt; TO[++CNT] = x, val[cnt] = 0, next[cnt] = Head[y], head[y] = cnt;     } bool BFs () {int x, I;     memset (dis, 0, sizeof (DIS));     while (!q.empty ()) Q.pop ();     Dis[s] = 1, Q.push (s);         while (!q.empty ()) {x = Q.front (), Q.pop (); for (i = head[x]; i; i = Next[i]) {if (Val[i] &&!dis[to[i]]) {D                 Is[to[i]] = dis[x] + 1;                 if (to[i] = = t) return 1;             Q.push (To[i]); }}} return 0;     } int Dinic (int x, int low) {if (x = = t) return low;     int temp = Low, I, K; for (i= Head[x]; I  i = Next[i]) {if (Val[i] && dis[to[i]] = = Dis[x] + 1) {k = Dinic (To[i], min (temp             , Val[i]));             if (!k) dis[to[i]] = 0;             Val[i]-= k, val[i ^ 1] + = k; if (! (         temp = k)) break; }} return low-temp;     } int main () {int n, m, I, J, X, tot, ans = 0;     scanf ("%d%d", &n, &m), s = 0, t = tot = n * m + 1;     for (i = 1; I <= n; i + +) for (j = 1; j <= M; j + +) scanf ("%d", &x), add (S, POS (i, J), X), ans + = x; for (i = 1; I <= n; i + +) for (j = 1; j <= M; j + +) scanf ("%d", &x), add (POS (i, J), T, X), ans + =     X  for (i = 1; i < n; i + +) for (j = 1; j <= M; j + +) scanf ("%d", &x), add (S, ++tot, X), Add (Tot, POS (i,     j), INF), Add (Tot, POS (i + 1, j), INF), ans + = x; for (i = 1; i < n; i + +) for (j = 1; j <= M; j + +) scanf ("%d", &x), add (POS (i, J), ++tot, INF), Add (PO s (i + 1, j) , tot, INF), add (Tot, T, X), ans + = x;  for (i = 1; I <= n; i + +) for (j = 1; j < M; j + +) scanf ("%d", &x), add (S, ++tot, X), Add (Tot, POS (i,     j), INF), Add (Tot, POS (i, J + 1), INF), ans + = x; for (i = 1; I <= n; i + +) for (j = 1; j < M; j + +) scanf ("%d", &x), add (POS (i, J), ++tot, INF), Add (PO     S (i, J + 1), Tot, INF), add (Tot, T, X), ans + = x;     while (BFS ()) ans-= Dinic (s, INF);     printf ("%d\n", ans); return 0; }

"bzoj2127" Happiness Network flow Minimum 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.