loj6007"Network Stream 24 questions" grid Fetch number network flow

Source: Internet
Author: User

Topic Portal

The problem of solving ideas and "" Bzoj "1497 [NOI2006] The biggest profit" the question is similar.

Because if a grid is selected, and the grid of four connected to the grid can not be selected, then we can dye the whole board black and white color and build the edge: All Black squares and source points to build a flow of the number of squares in the edge. All white squares and sinks create a flow that is the edge of the number in the grid. All Black squares with their four-unicom white squares Create a flow of +∞+\infty edges.

Then the new figure to do the smallest cut, that is, the maximum flow, cut the source point and the black square of the edge of the black box to give up the number, cut the white square and the edge of the meeting point to give up the number of white squares, the final checkerboard all the number and minus the maximum flow is the answer.

Attach the AC code:

#include <cstdio> #include <queue> #include <cstring> using namespace std;
const int N=1000,DX[5]={0,1,0,-1,0},DY[5]={0,0,1,0,-1}; struct side{int to,w,nt;}
int N,m,x,st,ed,map[31][31],num,h[n],ans;
Queue <int> que;

int d[n],cur[n];
    inline void Add (int x,int y,int W) {s[num]= (side) {y,w,h[x]},h[x]=num++;
S[num]= (side) {x,0,h[y]},h[y]=num++;
    } inline bool BFs (int st,int ed) {while (!que.empty ()) Que.pop ();
    memset (d,0,sizeof D), D[st]=1,que.push (ST);
        while (!que.empty ()) {int P=que.front (); Que.pop ();
        for (int i=h[p]; ~i; i=s[i].nt) if (s[i].w&&!d[s[i].to]) D[s[i].to]=d[p]+1,que.push (s[i].to);
    if (d[ed]) return 1;
} return 0;
    } inline int so (int x,int W) {if (x==ed) return w;
    int sum=0,f; for (int &i=cur[x]; ~i; i=s[i].nt) if (s[i].w&&d[s[i].to]==d[x]+1&& (F=so (S[i].to,min (w-sum,s[i
         ].W))) {s[i].w-=f,s[i^1].w+=f,sum+=f;   if (sum==w) return sum;
    } if (!sum) d[x]=0;
return sum;
    } #define WZ (x, y) ((x-1) *m+y) int main (void) {scanf ("%d%d", &n,&m), St=0,ed=wz (n,m) +1,memset (h,-1,sizeof h);
            for (int i=1; i<=n; ++i) for (int j=1; j<=m; ++j) {scanf ("%d", &x), ans+=x;
                if ((i+j)%2==0) {Add (St,wz (I,J), x);
                    for (int k=1; k<=4; ++k) {int fx=i+dx[k],fy=j+dy[k]; if (fx<1| | fx>n| | fy<1| |
                    FY&GT;M) continue;
                Add (WZ (i,j), WZ (Fx,fy), 2e9);
        }} else Add (WZ (i,j), ed,x);
    } while (BFS (st,ed)) memcpy (cur,h,sizeof h), Ans-=so (ST,0X7FFFFFFF);
return printf ("%d\n", ans), 0; }

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.