Hdoj 3657 Game "min-Cut box fill-in enhanced version"

Source: Internet
Author: User



GameTime limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1076 Accepted Submission (s): 456


Problem Descriptiononmylove have invented a game on nxm grids. There is one positive an integer on each grid. Now your can take the numbers from the grids to make your final score as high as possible. The the-the-get score is-like
The following:
At the beginning, the score is 0;
If you take a number which equals to X, the score increase x;
If There appears the neighboring empty grids after you taken the number and then the score should is decreased by 2 (x&y ). Here x and Y is the values used to existed on these and grids. Please pay attention this "neighboring grids" means there exits and only exits one common border between these, grids.

Since Onmylove thinks this problem are too easy, he adds one more rule:
Before you start the game, you is given some positions and the numbers on these positions must is taken away.
Can onmylove to Calculate:what's the highest score onmylove can get in the game?
Inputmultiple input cases. For each case, there is three integers n, m, K in a line.
N and M describing the size of the grids is nxm. K means there be k positions of which you must take their numbers. Then following n lines, each contains m numbers, representing the numbers on the NXM grids. then k lines follow. Each line contains integers, representing the row and column of one position
And you must take the number on this position. Also, the rows and columns is counted start from 1.
Limits:1≤n, M≤50, 0≤K≤NXM, the integer in every gird are not more than 1000.
Outputfor Each test case, the output of the highest score on one line.

Sample Input
2 2 12 22 21 12 2 12 74 11 1

Sample Output
HintAs to the second case in Sample Input, Onmylove gan get the highest score when calulating like This:2 + 7 + 4-2X (2&4)-2x (2&7) = 13-2x0-2x2 = 9.

Authoronmylove

Finally, I understand! It is recommended to check the number of squares first (2)


Test instructions: There is a n*m square and each lattice has a certain value. Initially your score is 0, you can arbitrarily select and take away a value from a square, and the corresponding score will be added to that value. If you choose to take the value of the two adjacent squares, the score will be subtracted by 2 * (x & y), where x and Y are the values of two adjacent squares. and give you a request-you have to take the value of the K-lattice. Ask the maximum score you can get.


Idea: Construct two-dimensional graph, then establish the minimum cut model to solve.


Do the thing to understand the place!

For the path source, x-, Y-sink, we discuss the meaning of the edges discussion based on the minimum cut.

First, if you cut the source, X, indicating that the x point selected Y point;

Second, if cut to Y->sink, indicating not selected Y point selected X point;

Third, if cut to X->y, indicating that both x point and select Y point.


As for why the construction of the binary map, I do not explain, just say the map.


Build: Set the super source point sources, the super sink point sink, the horizontal ordinate and the odd point set as the S set, the horizontal ordinate and the even point set as the T set.

1,source to the points inside the S set to build the edge, the capacity of the point right, indicating the cost of not selecting the point paid;

2,t set inside the point to sink built edge, the capacity of the point right, indicating the cost of not selecting the point to pay;

The points inside the 3,s set are built on the points inside the T-set near it, with a capacity of 2 * (x & y), and X and Y as two points, indicating the price to choose two points.

4, for the point that must be selected. If it is in the S set, then the source is built to it, the capacity is infinite, indicating that the cost of not selecting this point is infinite, so it must be selected. Similarly, if it is t set, it is built to sink, and the capacity is infinite.


Run the maximum flow-minimum cut, then subtract the sum of the values.



AC Code:

#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define MAXN 3000# Define MAXM 50000#define INF 0x3f3f3f3fusing namespace std;struct edge{int from, to, cap, flow, next;};    Edge Edge[maxm];int HEAD[MAXN], Edgenum;int DIST[MAXN], Cur[maxn];bool vis[maxn];int N, M, K;void init () {edgenum = 0; Memset (Head,-1, sizeof (head));} int point (int x, int y) {return (x-1) * M + y;}    void Addedge (int u, int v, int w) {Edge E1 = {u, V, W, 0, Head[u]};    Edge[edgenum] = E1;    Head[u] = edgenum++;    Edge E2 = {V, u, 0, 0, Head[v]};    Edge[edgenum] = E2; HEAD[V] = edgenum++;} int sum;//record numeric sum int map[60][60];bool judge (int x, int y) {return x >= 1 && x <= N && y >= 1 &A mp;& y <= M && (x + y)% 2 = = 0;}    int source, sink;//super sink point, void Getmap () {int A, B;    Source = 0, sink = n*m+1;    sum = 0; for (int i = 1, i <= N; i++) {for (int j = 1; J <= M; j + +) {scanf ("%D ", &map[i][j]);            Sum + = Map[i][j];            if ((i + j) & 1)//odd Addedge (source, point (I, J), Map[i][j]);        else//even Addedge (Point (I, J), Sink, map[i][j]);        }} while (k--) {scanf ("%d%d", &a, &b); if ((A + B) & 1) Addedge (source, point (A, B), INF);//Must be selected the edge cannot be cut off else Addedge (Point (A, B)    , sink, INF);    } int Move[4][2] = {0, 1, 0,-1, 1, 0, -1,0};            for (int i = 1, i <= N; i++) {for (int j = 1; J <= M; j + +) {if ((i + j) & 1)                    {for (int k = 0; k < 4; k++) {int x = i + move[k][0];                    int y = j + move[k][1];                if (judge (x, Y)) the point of the//s set of points to the T-set is built on the edge of the Addedge (spot (I, j), Dot (x, y), (Map[i][j] & Map[x][y]));    }}}}}bool BFS (int s, int t) {queue<int> Q; memset (Dist,-1, sizeof (Dist));    Memset (Vis, false, sizeof (VIS));    Dist[s] = 0;    Vis[s] = true;    Q.push (s); while (!        Q.empty ()) {int u = q.front ();        Q.pop ();            for (int i = head[u]; i =-1; i = Edge[i].next) {Edge E = Edge[i];                if (!vis[e.to] && e.cap > E.flow) {dist[e.to] = Dist[u] + 1;                if (e.to = = t) return true;                Vis[e.to] = true;            Q.push (e.to); }}} return false;}    int DFS (int x, int a, int t) {if (x = = T | | a = = 0) return A;    int flow = 0, F;        for (int &i = cur[x]; i =-1; i = Edge[i].next) {Edge &e = Edge[i]; if (dist[e.to] = = Dist[x] + 1 && (f = DFS (e.to, Min (A, e.cap-e.flow), T)) > 0) {edge[i].flow            + = f;            Edge[i^1].flow-= f;            Flow + + F;            A-= f;        if (a = = 0) break; }} return flow;} int Maxflow (int s, int t) {int FLow = 0;        while (BFS (s, t)) {memcpy (cur, head, sizeof (head));    Flow + = DFS (s, INF, T); } return flow;}        int main () {while (scanf ("%d%d%d", &n, &m, &k)! = EOF) {init ();        Getmap ();    printf ("%d\n", sum-maxflow (source, sink)); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj 3657 Game "min-Cut box fill-in enhanced version"

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.