hdu1569 Check Number (2) The maximum point weight independent set of two graphs

Source: Internet
Author: User

Test instructions: Chinese question.

Idea: First, according to the sum of the horizontal ordinate and the odd conversion into a bipartite graph, for (I, j) and it only conflict (I-1, J) (I, j-1) (i + 1, j) (I, J + 1) 4 squares,

Parity is reversed. If i + j is odd then and the surrounding 4 points are connected, then the problem is converted to a bit of power and-the minimum point weight coverage of the binary graph. We focus on the minimum point weight coverage

Model, the establishment of super-starting St, super-end Ed, for the left point of the dichotomy (I+j is odd), from the St to point a side, the edge of the weight of the point, for the right side of the second graph

Point, one edge from the point to Ed, and the right edge for that point. For the side of the two-dimensional graph, the edge of the INF, the minimum cut is the smallest point of the second graph of the right to cover, I

We also know that the maximum flow = minimum cut, then the maximum flow to the new diagram. See the code:

/********************************************************* file Name:hdu1569.cpp Author:kereo create time:2015 year January 20th Tuesday 17:21 28 sec *********************************************************/#include <iostream> #include < cstdio> #include <cstring> #include <queue> #include <set> #include <map> #include <vector > #include <stack> #include <cmath> #include <string> #include <algorithm>using namespace std ; typedef long long ll;const int sigma_size=26;const int n=100+50;const int maxn=2500+50;const int Inf=0x3fffffff;const Dou ble eps=1e-8;const int mod=100000000+7; #define L (x) (x<<1) #define R (x) (x<<1|1) #define PII Pair<int, int& gt; #define MK (x, y) Make_pair ((×), ()) int m,n;int Edge_cnt,top;int HEAD[MAXN],QUE[MAXN],S[MAXN],DEP[MAXN],CUR[MAXN], gap[maxn];struct edge{int cap,flow,v,next;}    Edge[maxn*maxn];void init () {edge_cnt=top=0; memset (head,-1,sizeof (Head));}    void Addedge (int u,int v,int W) {edge[edge_cnt].v=v; EDge[edge_cnt].cap=w;    edge[edge_cnt].flow=0; Edge[edge_cnt].next=head[u];    head[u]=edge_cnt++;    Edge[edge_cnt].v=u; Edge[edge_cnt].cap=0;    edge[edge_cnt].flow=0; EDGE[EDGE_CNT].NEXT=HEAD[V]; head[v]=edge_cnt++;}    void BFs (int st,int ed) {memset (gap,0,sizeof (GAP));    memset (dep,-1,sizeof (DEP));     int front=0,rear=0; Gap[0]=1; dep[ed]=0;    que[rear++]=ed;        while (front!=rear) {int u=que[front++];            for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v;            if (dep[v]!=-1) continue;            Que[rear++]=v; dep[v]=dep[u]+1;        gap[dep[v]]++;    }}}int isap (int st,int ed) {BFS (st,ed);    memcpy (cur,head,sizeof (head));    int ans=0,u=st;            while (dep[st]<n*m+2) {if (U = = ed) {int min=inf,inser; for (int i=0;i<top;i++) {if (edge[s[i]].cap-edge[s[i]].flow<min) {Min=edge[s[i]].cap                    -edge[s[i]].flow;                Inser=i;     }       } for (int i=0;i<top;i++) edge[s[i]].flow+=min,edge[s[i]^1].flow-=min; Ans+=min;            Top=inser;            U=EDGE[S[TOP]^1].V;        Continue        } int flag=0,v;            for (int i=cur[u];i!=-1;i=edge[i].next) {v=edge[i].v;                if (edge[i].cap-edge[i].flow>0 && dep[v]+1 = = Dep[u]) {flag=1; cur[u]=i;            Break            }} if (flag) {s[top++]=cur[u]; u=v;        Continue        } int d=n*m+2;            for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v;        if (edge[i].cap-edge[i].flow>0 && dep[v]<d) cur[u]=i,d=dep[v];         } gap[dep[u]]--;        if (!gap[dep[u]]) return ans; dep[u]=d+1;        gap[dep[u]]++;    if (u!=st) u=edge[s[--top]^1].v; } return ans;        int main () {while (~scanf ("%d%d", &m,&n)) {init ();    int sum=0,st=m*n,ed=m*n+1,w;     for (int i=0;i<m;i++) {for (int j=0;j<n;j++) {scanf ("%d", &w); sum+=w;                     if ((i+j)%2) {Addedge (st,i*n+j,w);                    if (i) Addedge (I*n+j, (i-1) *n+j,inf);                    if (j) Addedge (I*n+j,i*n+j-1,inf);                    if (i!=m-1) Addedge (I*n+j, (i+1) *n+j,inf);                if (j!=n-1) Addedge (I*n+j,i*n+j+1,inf);            } else Addedge (I*N+J,ED,W);    }} printf ("%d\n", Sum-isap (st,ed)); }return 0;}


hdu1569 Check Number (2) The maximum point weight independent set of two graphs

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.