[Tyvj] QQ Farm (max stream + max weight closure diagram)

Source: Internet
Author: User

Http://tyvj.cn/Problem_Show.aspx? Id = 1338

The time is in the rank7 period. Isap I write so small constants...

View my other blog on the max permission closure Diagram

This is an obvious model.

First, we first dye the entire graph to make it black and white. Here we only need to link the white nodes to the upper and lower sides at the Black Point. Obviously, these nodes have the right value, what we need to require is the maximum authority, so we can do it just like my blog post.

Sum-least cut is the answer.

#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 max(a,b) ((a)>(b)?(a):(b))#define min(a,b) ((a)<(b)?(a):(b))#define read(a) a=getnum()#define print(a) printf("%d", a)inline int getnum() { int ret=0; char c; for(c=getchar(); c<‘0‘ || c>‘9‘; c=getchar()); for(; c>=‘0‘ && c<=‘9‘; c=getchar()) ret=ret*10+c-‘0‘; return ret; }const int N=40005, M=N*4*2, oo=~0u>>2;int ihead[N], inext[M], from[M], to[M], cap[M], cnt=1;int gap[N], d[N], cur[N], p[N];inline void add(const int &u, const int &v, const int &w) {inext[++cnt]=ihead[u]; ihead[u]=cnt; from[cnt]=u; to[cnt]=v; cap[cnt]=w;inext[++cnt]=ihead[v]; ihead[v]=cnt; from[cnt]=v; to[cnt]=u; cap[cnt]=0;}inline int isap(const int &s, const int &t, const int &n) {int u=s, f, i, flow=0;for1(i, 0, n) cur[i]=ihead[i];gap[0]=n;while(d[s]<n) {for(i=cur[u]; i; i=inext[i]) if(cap[i] && d[u]==d[to[i]]+1) break;if(i) {cur[u]=i; p[to[i]]=i; u=to[i];if(u==t) {for(f=oo; u!=s; u=from[p[u]]) f=min(f, cap[p[u]]);for(u=t; u!=s; u=from[p[u]]) cap[p[u]]-=f, cap[p[u]^1]+=f;flow+=f;}}else {if(!(--gap[d[u]])) break;d[u]=n;cur[u]=ihead[u];for(i=ihead[u]; i; i=inext[i]) if(cap[i] && d[u]>d[to[i]]+1) d[u]=d[to[i]]+1;++gap[d[u]];if(u!=s) u=from[p[u]];}}return flow;}int main() {int t, u, n;read(n);int S=0, T=n*n+1;int sum=0, last;for1(i, 1, n) {last=i;for1(j, 1, n) {read(t); sum+=t;u=(i-1)*n+j;if(last%2) {if(i>1) add(u, u-n, oo);if(i<n) add(u, u+n, oo);if(j>1) add(u, u-1, oo);if(j<n) add(u, u+1, oo);add(S, u, t);}else add(u, T, t);++last;}}printf("%d\n", sum-isap(S, T, T+1));return 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.