记录编号 154115 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 [国家集训队2011]happiness(吴确) 最终得分 100
用户昵称 Gravatarztx 是否通过 通过
代码语言 C++ 运行时间 2.437 s
提交时间 2015-03-21 15:58:39 内存使用 5.44 MiB
显示代码纯文本
/****************************************\
* Author : ztx
* Title  : [bzoj] 2127: happiness
* ALG    : 网络流最小割
* CMT    :
* Time   :
\****************************************/

#include <cstdio>
#define Rep(i,l,r) for(i=(l);i<=(r);i++)
#define Rev(i,r,l) for(i=(r);i>=(l);i--)
int CH , NEG ;
template <typename TP>
inline void read(TP& ret) {
    ret = NEG = 0 ; while (CH=getchar() , CH<'!') ;
    if (CH == '-') NEG = true , CH = getchar() ;
    while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ;
    if (NEG) ret = -ret ;
}
template <typename TP>
inline void reads(TP& ret) {
	while (ret=getchar() , ret<'!') ;
	while (CH=getchar() , CH>'!') ;
}

#include <cstring>

#define  maxN  50050LL
#define  maxM  200020LL
#define  infi  0x7f7f7f7fLL

struct FST { int to , next ; int flow ; } e[maxM<<1] ;
int star[maxN] = {0} , tote = 1 ;
inline void FST_init() { memset(star , 0 , sizeof star) ; tote = 1 ; }
inline void AddEdge(int u , int v , int cap) {
	e[++tote].to = v ; e[tote].flow = cap ; e[tote].next = star[u] ; star[u] = tote ;
	e[++tote].to = u ; e[tote].flow = 0 ; e[tote].next = star[v] ; star[v] = tote ;
}

#define  min(x,y) ((x)<(y)?(x):(y))
int N , S , T ;
int h[maxN] , vh[maxN] ;

int dfs(int u , int flowu) {
int p , tmp = h[u]+1 ;
int flow = 0 , flowv ;
	if (u == T) return flowu ;
	for (p = star[u] ; p ; p = e[p].next) {
		if (e[p].flow && (h[e[p].to]+1==h[u])) {
			flowv = dfs(e[p].to , min(flowu-flow , e[p].flow)) ;
			flow += flowv ; e[p].flow -= flowv ; e[p^1].flow += flowv ;
			if (flow==flowu || h[S]==N) return flow ;
		}
	}
	for (p = star[u] ; p ; p = e[p].next)
		if (e[p].flow) tmp = min(tmp , h[e[p].to]) ;
	if (--vh[h[u]] == 0) h[S] = N ;
	else ++ vh[h[u]=tmp+1] ;
	return flow ;
}

int SAP() {
	int ret = 0 ;
	memset(vh , 0 , sizeof vh) ;
	memset(h , 0 , sizeof h) ;
	vh[S] = N ;
	while (h[S] < N) ret += dfs(S , infi) ;
	return ret ;
}

int n , m ;

int main() {
int i , j , w , tot , ans ;
	#define READ
	#ifdef  READ
		freopen("nt2011_happiness.in" ,"r",stdin ) ;
		freopen("nt2011_happiness.out","w",stdout) ;
	#endif
	read(n) , read(m) ;
	S = n*m+n*(m-1)*2+(n-1)*m*2+1 ;
	T = N = S+1 ;
	tot = ans = 0 ;
	/*S 为选文*/
	Rep (i,1,n) Rep (j,1,m) {
		read(w) ;
		ans += w ;
		AddEdge(S , (i-1)*m+j , w) ;
	}
	Rep (i,1,n) Rep (j,1,m) {
		read(w) ;
		ans += w ;
		AddEdge((i-1)*m+j , T , w) ;
	}
	tot = n*m ;
	Rep (i,1,n-1) Rep (j,1,m) {
		read(w) ;
		ans += w ;
		tot ++ ;
		AddEdge(S , tot , w) ;
		AddEdge(tot , (i-1)*m+j , infi) ;
		AddEdge(tot , i*m+j , infi) ;
	}
	Rep (i,1,n-1) Rep (j,1,m) {
		read(w) ;
		ans += w ;
		tot ++ ;
		AddEdge(tot , T , w) ;
		AddEdge((i-1)*m+j , tot , infi) ;
		AddEdge(i*m+j , tot , infi) ;
	}
	Rep (i,1,n) Rep (j,1,m-1) {
		read(w) ;
		ans += w ;
		tot ++ ;
		AddEdge(S , tot , w) ;
		AddEdge(tot , (i-1)*m+j , infi) ;
		AddEdge(tot , (i-1)*m+j+1 , infi) ;
	}
	Rep (i,1,n) Rep (j,1,m-1) {
		read(w) ;
		ans += w ;
		tot ++ ;
		AddEdge(tot , T , w) ;
		AddEdge((i-1)*m+j , tot , infi) ;
		AddEdge((i-1)*m+j+1 , tot , infi) ;
	}
	ans -= SAP() ;
	printf("%d\n", ans) ;
	#ifdef  READ
		fclose(stdin) ; fclose(stdout) ;
	#else
		getchar() ; getchar() ;
	#endif
	return 0 ;
}