# BZOJ2127Happiness

#### 题解

$S_x+S_y=w_x+w_y+w‘$

$T_x+T_y=l_x+l_y+l'$

$S_x+w1+T_y=w_y+l_+w'+l'$

$S_y+w2+T_x=w_x+l_y+w'+l'$

#### 代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define N 30009
#define M 109
#define inf 1e9
using namespace std;
queue<int>q;
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
long long ans;
inline int rd(){
int x=0;char c=getchar();bool f=0;
while(!isdigit(c)){if(c=='-')f=1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
return f?-x:x;
}
struct edge{
int n,to,l;
}e[N*30];
inline void add(int u,int v,int l,int tag){
}
bool bfs(int s,int t){
memset(deep,0,sizeof(deep));
q.push(s);deep[s]=1;
while(!q.empty()){
int u=q.front();q.pop();
int v=e[i].to;
if(!deep[v]&&e[i].l){
deep[v]=deep[u]+1;
q.push(v);
}
}
}
return deep[t];
}
int dfs(int u,int t,int l){
if(u==t||!l)return l;
int flow=0,f;
for(int &i=cur[u];i;i=e[i].n){
int v=e[i].to;
if(deep[v]==deep[u]+1&&(f=dfs(v,t,min(l,e[i].l)))){
e[i].l-=f;e[i^1].l+=f;flow+=f;l-=f;
if(!l)break;
}
}
return flow;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)a[i][j]=rd()*2,id[i][j]=++num,ans+=a[i][j];
int S=0,T=num+1;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)s[i][j]=rd()*2,ans+=s[i][j];
for(int i=1;i<n;++i)
for(int j=1;j<=m;++j){
s_a[i][j]=rd();ans+=s_a[i][j]*2;
a[i][j]+=s_a[i][j],a[i+1][j]+=s_a[i][j];
}
for(int i=1;i<n;++i)
for(int j=1;j<=m;++j){
s_s[i][j]=rd();ans+=s_s[i][j]*2;
s[i][j]+=s_s[i][j];s[i+1][j]+=s_s[i][j];
}
for(int i=1;i<=n;++i)
for(int j=1;j<m;++j){
s_a[i][j]=rd();ans+=s_a[i][j]*2;
a[i][j]+=s_a[i][j],a[i][j+1]+=s_a[i][j];
}
for(int i=1;i<=n;++i)
for(int j=1;j<m;++j){
s_s[i][j]=rd();ans+=s_s[i][j]*2;
s[i][j]+=s_s[i][j];s[i][j+1]+=s_s[i][j];