[Wikioi] 3 squares (maximum stream + maximum weight closed subgraph)

Source: Internet
Author: User

 

At the beginning, I thought of the pressure and saw n <= 30 give up.

Then I thought of black and white dyeing, and then I lost my brains. I didn't expect how to connect to the edge.

Simple question

After black and white dyeing, each vertex is connected to four sides, with a capacity of OO. If it is a black (white) node, the source node is connected to one edge, and the capacity is the weight. If it is a White (Black) node, connects an edge to the sink, and the capacity is the weight.

The final answer is all the lattice values and the-maximum stream (actually the smallest cut)

PS: (in fact, it is the QQ Farm that I have done before .. Http://www.cnblogs.com/iwtwiioi/p/3893519.html

Why do I do this by referring to my previous blog (http://www.cnblogs.com/iwtwiioi/p/3872099.html)

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>using namespace std;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << #x << " = " << x << endl#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }const int N=1000, M=N<<4, oo=~0u>>1;struct ED { int u, v, next, cap; } e[M];int n, m, cnt=1;int d[N], gap[N], cur[N], p[N], ihead[N];inline int id(const int &x, const int &y) { return (x-1)*m+y; }inline void add(const int &u, const int &v, const int &c) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].v=v; e[cnt].u=u; e[cnt].cap=c;e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].v=u; e[cnt].u=v; e[cnt].cap=0;}int isap(const int &s, const int &t, const int &n) {int ret=0, f, u, v, i;for1(i, 0, n) cur[i]=ihead[i];gap[0]=n; u=s;while(d[s]<n) {for(i=cur[u]; i; i=e[i].next) if(e[i].cap && d[u]==d[e[i].v]+1) break;if(i) {v=e[i].v; p[v]=cur[u]=i; u=v;if(u==t) {for(f=oo; u!=s; u=e[p[u]].u) f=min(f, e[p[u]].cap);for(u=t; u!=s; u=e[p[u]].u) e[p[u]].cap-=f, e[p[u]^1].cap+=f;ret+=f;}}else {if(! (--gap[d[u]]) ) break;d[u]=n;cur[u]=ihead[u];for(i=ihead[u]; i; i=e[i].next) if(e[i].cap && d[u]>d[e[i].v]+1) d[u]=d[e[i].v]+1;++gap[d[u]];if(u!=s) u=e[p[u]].u;}}return ret;}int main() {read(n); read(m);int ans=0, t, now, S=id(n, m)+1, T=id(n, m)+2;for1(i, 1, n) for1(j, 1, m) {read(t); ans+=t;now=id(i, j);if((i+j)%2) add(S, now, t);else add(now, T, t);if(j<m) add(now, now+1, oo), add(now+1, now, oo);if(i<n) add(now, id(i+1, j), oo), add(id(i+1, j), now, oo);}print(ans-isap(S, T, T));return 0;}

 

 

 

 

 

Description Description

«Problem description:
In a checker with M * n squares, each square has a positive integer. We need to take the number from the square
The square where the number 2 is located does not have a public edge, and the sum of the retrieved numbers is the largest. Design a number fetch algorithm that meets the requirements.
«Programming task:
For a given checker, program the number to find the maximum sum.

Input description Input description

Row 1st has two positive integers m and n, indicating the number of rows on the board respectively.
And the number of columns. In the next m row, each row has n positive integers, indicating the number in the checker square.

Output description Output description

Output the maximum sum of the number.

Sample Input Sample Input

3 3
1 2 3
3 2 3
2 3 1

Sample output Sample output

11

Data range and prompt Data size & hint

N, m <= 30

 

[Wikioi] 3 squares (maximum stream + maximum weight closed subgraph)

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.