[Bzoj 2127] Happiness "min cut"

Source: Internet
Author: User

Title Link: BZOJ-2127

Problem analysis

First of all, everyone either studies the arts, or science, so it can be thought of as a minimal cut model.

We are determined that if a person and S are connected to the text, if the link with T is the theory.

So let's set up the map again. The first use of the minimum cut is to first add all the possible weights, minus the minimum cut (i.e. the weight that cannot be obtained).

If a person's theory, it is necessary to cut off the side of the S-linked, then is to cut off the proceeds of the learning text. So, for each point, from S to it, the weight value is the benefit of the learning text.

Similarly, for each point, from it to the T-link, the weight value is its theoretical benefit.

For the two neighboring people, they have the benefit of learning the text at the same time and the benefit of the academic theory simultaneously.

If they all learn the text, they will lose the same theoretical benefits, so from their respective to the T-side, the right value for them at the same time half of the theoretical gains.

In the same vein, from S to them, respectively, the weight of the value of their learning at the same time half of the proceeds.

But if a person learns the theory of a person, it is necessary to lose at the same time learn the text and at the same time the theory of income, so before they even two-way edge, the right value for the same time learn the benefits of the text plus the same theoretical income of half.

This makes it possible to intelligently control the different benefits.

Because half of the weights may not be integers, the weights are multiplied by 2 and then divided by 2.

Code
#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath > #include <algorithm>using namespace std;const int maxn = 5, Maxnode = 10000 + +, INF = 999999999;int N, m , Sum, S, T, Tot, Maxflow;int IDX[MAXN][MAXN], wk[maxn][maxn][5], lk[maxn][maxn][5], Num[maxnode], d[maxnode];struct Edge {int V, W; Edge *next, *other;} E[maxnode *], *p = E, *point[maxnode], *last[maxnode];inline void Addedge (int x, int y, int z) {Edge *q = ++p; ++p; P-v = y; P-w = z; P-Next = point[x]; POINT[X] = P; P, other = Q; Q-v = x; Q-W = 0; Q-Next = Point[y]; Point[y] = Q; Q, other = P;} inline int gmin (int a, int b) {return a < b a:b;} int DFS (int now, int Flow) {if (now = = T) return flow;int ret = 0;for (Edge *j = Last[now]; j; j = J-Next) if (J-&gt ; W && d[now] = = D[j v] + 1) {Last[now] = J;int p = DFS (J-V, Gmin (J, W, Flow-ret)); ret + = P; J-&G T W-= p; J-Other W + = p;if (ret = = Flow) return ret; if (D[s] >= Tot) return ret;if (--num[d[now]] = = 0) D[s] = Tot;++num[++d[now]]; Last[now] = Point[now];return ret;}  int main () {scanf ("%d%d", &n, &m); for (int i = 1; I <= n; ++i) for (int j = 1; j <= m; ++j) idx[i][j] = (i-1) * M + j; S = n * m + 1; T = n * m + 2; Tot = t;for (int i = 1; I <= n; ++i) for (int j = 1; j <= m; ++j) scanf ("%d", &wk[i][j][0]); for (int i = 1; i < = N; ++i) for (int j = 1; j <= m; ++j) scanf ("%d", &lk[i][j][0]); for (int i = 1; I <= n-1; ++i) for (int j = 1; J < = m; ++J) scanf ("%d", &wk[i][j][1]); for (int i = 1; I <= n-1; ++i) for (int j = 1; j <= m; ++j) scanf ("%d", &lk[i] [j] [1]);  for (int i = 1; I <= n; ++i) for (int j = 1; j <= m-1; ++j) scanf ("%d", &wk[i][j][2]); for (int i = 1; I <= N; ++i) for (int j = 1; j <= m-1; ++j) scanf ("%d", &lk[i][j][2]); int v1, v2, v3;for (int i = 1; I <= n; ++i) for (i NT J = 1; J <= M; ++J) {Sum + = Wk[i][j][0] + WK[I][J][1] + wk[i][j][2]; Sum + = Lk[i][j][0] + lk[i][j][1] + LK[I][J][2];V1 = wk[i][j][0] * 2 + wk[i][j][1] + wk[i][j][2]; V2 = lk[i][j][0] * 2 + lk[i][j][1] + lk[i][j][2];v1 + + wk[i-1][j][1]; V2 + = Lk[i-1][j][1];v1 + = wk[i][j-1][2]; V2 + = lk[i][j-1][2]; Addedge (S, idx[i][j], v1); Addedge (Idx[i][j], T, v2); if (I < n) {v3 = wk[i][j][1] + lk[i][j][1]; Addedge (Idx[i][j], Idx[i + 1][j], v3); Addedge (Idx[i + 1][j], idx[i][j], v3);} if (J < m) {v3 = wk[i][j][2] + lk[i][j][2]; Addedge (Idx[i][j], idx[i][j + 1], V3); Addedge (Idx[i][j + 1], idx[i][j], V3);}} Maxflow = 0;memset (d, 0, sizeof (d)), memset (num, 0, sizeof (num)); Num[0] = tot;for (int i = 1; I <= Tot; ++i) Last[i] = Point[i];while (D[s] < Tot) Maxflow + = DFS (S, INF); Sum-= Maxflow >> 1;printf ("%d\n", sum); 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.