# bzoj 2127: happiness

1 2
1 1
100 110
1
1000

1210
【样例说明】

【数据规模】

## Source

// MADE BY QT666
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
#define RG register
using namespace std;
typedef long long ll;
const int N=1000050;
const int Inf=19260817;
const double eps=1e-5;
int gi(){
int x=0,flag=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') flag=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*flag;
}
double s[N],F;
int id[200][200],a[200][200],b[200][200],v1[200][200],v2[200][200],v3[200][200],v4[200][200];
int valt;
inline void Addedge(RG int x,RG int y,RG double z) {
}
inline void lnk(RG int x,RG int y,RG double z){
}
inline bool bfs(){
for(RG int i=S;i<=T;i++) level[i]=0;
q[0]=S,level[S]=1;int t=0,sum=1;
while(t<sum){
int x=q[t++];
if(x==T) return 1;
int y=to[i];
if(s[i]-0>=eps&&level[y]==0){
level[y]=level[x]+1;
q[sum++]=y;
}
}
}
return 0;
}
inline double dfs(RG int x,double maxf){
if(x==T) return maxf;
double ret=0;
int y=to[i];double f=s[i];
if(level[y]==level[x]+1&&f-0>=eps){
double minn=min(f,maxf-ret);
f=dfs(y,minn);
s[i]-=f,s[i^1]+=f,ret+=f;
if(ret==maxf) break;
}
}
if(!ret) level[x]=0;
return ret;
}
inline void Dinic(){
while(bfs()) F+=dfs(S,Inf);
}
int main(){
n=gi(),m=gi();S=0,T=n*m+1;
for(RG int i=1;i<=n;i++)
for(RG int j=1;j<=m;j++)
id[i][j]=++tt;
for(RG int i=1;i<=n;i++)
for(RG int j=1;j<=m;j++)
a[i][j]=gi(),lnk(S,id[i][j],a[i][j]),valt+=a[i][j];
for(RG int i=1;i<=n;i++)
for(RG int j=1;j<=m;j++)
b[i][j]=gi(),lnk(id[i][j],T,b[i][j]),valt+=b[i][j];
for(int i=1;i<=n-1;i++)
for(RG int j=1;j<=m;j++)
v1[i][j]=gi(),valt+=v1[i][j];
for(RG int i=1;i<=n-1;i++)
for(RG int j=1;j<=m;j++)
v2[i][j]=gi(),valt+=v2[i][j];
for(RG int i=1;i<=n-1;i++)
for(RG int j=1;j<=m;j++){
lnk(S,id[i][j],v1[i][j]/2.0);lnk(S,id[i+1][j],v1[i][j]/2.0);
lnk(id[i][j],T,v2[i][j]/2.0);lnk(id[i+1][j],T,v2[i][j]/2.0);
lnk(id[i][j],id[i+1][j],(v1[i][j]+v2[i][j])/2.0);
lnk(id[i+1][j],id[i][j],(v1[i][j]+v2[i][j])/2.0);
}
for(RG int i=1;i<=n;i++)
for(RG int j=1;j<=m-1;j++)
v3[i][j]=gi(),valt+=v3[i][j];
for(RG int i=1;i<=n;i++)
for(RG int j=1;j<=m-1;j++)
v4[i][j]=gi(),valt+=v4[i][j];
for(RG int i=1;i<=n;i++)
for(RG int j=1;j<=m-1;j++){
lnk(S,id[i][j],v3[i][j]/2.0);lnk(S,id[i][j+1],v3[i][j]/2.0);
lnk(id[i][j],T,v4[i][j]/2.0);lnk(id[i][j+1],T,v4[i][j]/2.0);
lnk(id[i][j],id[i][j+1],(v3[i][j]+v4[i][j])/2.0);
lnk(id[i][j+1],id[i][j],(v3[i][j]+v4[i][j])/2.0);
}
Dinic();printf("%d\n",valt-(int)F);
return 0;
}


posted @ 2017-06-07 08:23  qt666  阅读(128)  评论(0编辑  收藏  举报