Number of HDU 1569 squares (2)

Source: Internet
Author: User
Number of squares (2) time limit: 5000 msmemory limit: 32768 kbthis problem will be judged on HDU. Original ID: 1569
64-bit integer Io format: % i64d Java class name: Main gives you a checkboard of M * n, each of which has a non-negative number.
Take out several numbers from them, so that the lattice of any two numbers does not have a public edge. That is to say, the two grids of the obtained number cannot be adjacent and the sum of the obtained numbers is the largest. Input includes multiple test instances. Each test instance includes 2 integer m, n, and M * n non-negative numbers (M <= 50, n <= 50). Output for each test instance, maximum possible output and sample input
3 375 15 21 75 15 28 34 70 5
Sample output
188
Sourcehappy 2007 problem solving: Maximum vertex weight independence set = sum-minimum vertex weight independence set = Minimum Cut = maximum stream. Black/white board. I + J is an even point, which is directly connected to a super sink point. The weight is the weight of the Change Point. I + J is an odd vertex connected to a super Source Vertex and its weight is the vertex's weight. In addition, the weights are infinitely large when connected to neighboring four vertices.
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 3000;18 struct arc {19     int to,flow,next;20     arc(int x = 0,int y = 0,int z = 0) {21         to = x;22         flow = y;23         next = z;24     }25 };26 arc e[maxn*10];27 int head[maxn],d[maxn],tot;28 int S,T,q[maxn],hd,tail,n,m;29 void add(int u,int v,int flow) {30     e[tot] = arc(v,flow,head[u]);31     head[u] = tot++;32     e[tot] = arc(u,0,head[v]);33     head[v] = tot++;34 }35 bool bfs() {36     for(int i = 1; i <= T; i++) d[i] = 0;37     d[S] = 1;38     hd = tail = 0;39     q[tail++] = S;40     while(hd < tail) {41         int u = q[hd++];42         for(int i = head[u]; ~i; i = e[i].next) {43             if(e[i].flow && !d[e[i].to]) {44                 d[e[i].to] = d[u]+1;45                 q[tail++] = e[i].to;46             }47         }48     }49     return d[T];50 }51 int dfs(int u,int low) {52     int tmp = 0,f;53     if(u == T || !low) return low;54     for(int i = head[u]; ~i; i = e[i].next) {55         if(d[e[i].to] == d[u]+1 && (f = dfs(e[i].to,min(low,e[i].flow)))) {56             tmp += f;57             e[i].flow -= f;58             e[i^1].flow += f;59             low -= f;60             if(!low) break;61         }62     }63     return tmp;64 }65 int main() {66     while(~scanf("%d %d",&n,&m)) {67         memset(head,-1,sizeof(head));68         int sum = tot = 0,o,ans = 0;69         S = n*m+1;70         T = n*m+2;71         for(int i = 1; i <= n; i++)72             for(int j = 1; j <= m; j++) {73                 scanf("%d",&o);74                 sum += o;75                 int tmp = (i-1)*m + j;76                 if((i+j)&1) {77                     add(S,tmp,o);78                     if(i > 1) add(tmp,tmp-m,INF);79                     if(j > 1) add(tmp,tmp-1,INF);80                     if(i < n) add(tmp,tmp+m,INF);81                     if(j < m) add(tmp,tmp+1,INF);82                 } else add(tmp,T,o);83             }84         while(bfs()) ans += dfs(S,INF);85         printf("%d\n",sum-ans);86     }87     return 0;88 }
View code

 

Number of HDU 1569 squares (2)

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.