HDU 3657 Game (take the smallest cut) classic

Source: Internet
Author: User
Game Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1065 Accepted Submission (s): 449


Problem Description Onmylove 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?
Input multiple 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.
Output for each test case, output of the highest score on one line.

Sample Input
2 2 1 2 2 2 2 1 1 2 2 1 2 7 4 1 1 1
Sample Output
4 9 Hint As 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.
Author Onmylove
Source of Asia regional Chengdu Site--online Contest

Title Description: N*m matrix, each position has a positive number, the beginning of your score is 0, when you take a figure, your score increases that score. If you finish the number, and the new 2 are empty squares, then your score is reduced by 2 * (x & y), and X, Y is the original value of the two squares.

At the same time there are some additional conditions, some lattice numbers must be taken away.

Problem solving: Similar to the number of squares, note that more than two different conditions. 1. Take the adjacent lattice to reduce the (x&y) map, the adjacent two lattice building edge capacity of X&y 2. There are k lattices that must be taken, then the capacity of the edge of the point s or T connected to the point that must be taken is the INF. This will not be cut when the minimum cut is obtained.

#include <stdio.h> #include <string.h> #include <queue> #include <algorithm> using namespace std
;   #define CAPTYPE int const int MAXN = 100010;    Total number of points const int MAXM = 400010;
Total number of edges const int INF = 1<<30;
    struct edg{int to,next;
Captype Cap,flow;
} EDG[MAXM];
int EID,HEAD[MAXN];  int GAP[MAXN];  The number of points for each distance (or can be considered a height) int DIS[MAXN];  The shortest distance from each point to the end Enode int CUR[MAXN];

Cur[u] means that from the U point can flow through the cur[u] number side int PRE[MAXN];
    void Init () {eid=0;
memset (head,-1,sizeof (head));
    }//There are three parameters to the edge, 4 parameters void addedg (int u,int v,captype c,captype rc=0) with no forward edge {edg[eid].to=v; edg[eid].next=head[u]; Edg[eid].cap=c; edg[eid].flow=0;

    head[u]=eid++; Edg[eid].to=u;
    EDG[EID].NEXT=HEAD[V]; EDG[EID].CAP=RC; edg[eid].flow=0;
head[v]=eid++;
    } captype maxflow_sap (int snode,int enode, int n) {//n is the total number of points including the source and sink points, this must be noted memset (Gap,0,sizeof (GAP));
    memset (dis,0,sizeof (dis));
    memcpy (cur,head,sizeof (head));
    Pre[snode] =-1;
    Gap[0]=n;  Captype ans=0;Max Stream int U=snode;
            while (dis[snode]<n) {//determines from SNode point there is no flow to the next adjacent point if (u==enode) {//Find a way to add flow captype min=inf;
            int inser;
                for (int i=pre[u]; i!=-1; i=pre[edg[i^1].to])//To find the maximum amount of traffic Min if (min>edg[i].cap-edg[i].flow) {
                Min=edg[i].cap-edg[i].flow;
            Inser=i;
                } for (int i=pre[u]; i!=-1; i=pre[edg[i^1].to]) {edg[i].flow+=min;  Edg[i^1].flow-=min;
            Flow of the side that can be recycled} ans+=min;
            u=edg[inser^1].to;
        Continue  } bool flag = FALSE;
        It is possible to determine whether an int v can flow toward the neighboring point from the U point;
            for (int i=cur[u]; i!=-1; i=edg[i].next) {v=edg[i].to;
                if (edg[i].cap-edg[i].flow>0 && dis[u]==dis[v]+1) {flag=true;
                Cur[u]=pre[v]=i;
            Break
            }} if (flag) {u=v;
        Continue
}        If a flow adjacent point is not found above, then the distance from the starting point U (which can also be considered a height) is the minimum distance of +1 int mind= n for the adjacent flow point;
            for (int i=head[u]; i!=-1; i=edg[i].next) if (edg[i].cap-edg[i].flow>0 && mind>dis[edg[i].to]) {
            MIND=DIS[EDG[I].TO];
        Cur[u]=i;
        } gap[dis[u]]--;  if (gap[dis[u]]==0) return ans; When Dis[u] The point of this distance is gone, it is impossible to find an augmented stream path from the source point//Because there is only one distance from the sink point to the current point, the current point must pass from the source point to the sink point, but the current point is not able to find
        To the point that can flow, then the inevitable flow of dis[u]=mind+1;//if a stream of adjacent points is found, the distance is the distance of the adjacent point +1, if not found, then the n+1 gap[dis[u]]++;  if (U!=snode) u=edg[pre[u]^1].to;
Back one side} return ans;
    } int main () {int n,m,k,cost[55][55],flag[55][55];
    int dir[4][2]={0,1,0,-1,1,0,-1,0};
        while (scanf ("%d%d%d", &n,&m,&k) >0) {init ();
        int s=n*m,t=n*m+1, ans=0;
           for (int. i=0; i<n; i++) for (int j=0; j<m; J + +) {scanf ("%d", &cost[i][j]);
        ANS+=COST[I][J]; } int x,Y
        memset (flag,0,sizeof (flag));
            while (k--) {scanf ("%d%d", &x,&y); x--; y--;
        Flag[x][y]=1; } for (int i=0, i<n; i++) for (int j=0; j<m; J + +) if ((i+j) &1) {ADDEDG
            (s, I*m+j, Flag[i][j]==0?cost[i][j]:inf);
                 for (int e=0; e<4; e++) {x=i+dir[e][0];
                 Y=J+DIR[E][1]; if (x>=0&&x<n&&y>=0&&y<m) addedg (I*m+j, x*m+y,2* (cost[i][j]&cost[
             X][y]));
            
        }} else Addedg (I*m+j,t,flag[i][j]==0?cost[i][j]:inf);
        Ans-=maxflow_sap (S, T, t+1);
    printf ("%d\n", ans);
 }
}


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.