码迷,mamicode.com
首页 > 移动开发 > 详细

[国家集训队2011]happiness

时间:2018-05-24 19:30:05      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:oid   def   define   ESS   memset   algo   mat   sizeof   push   

pragma comment(linker, "/STACK:1024000000,1024000000")

include

include

include

include

include

include

define re register

define inf 400000000

define MAXN 10005

define MAXM 200001

using namespace std;
int n,s,q,dis[2000011],t,l,cur[200051],m,tot,cnt;
int id[101][101],a[101][101],b[101][101],a1[101][101],b1[101][101],a2[101][101],b2[101][101];
struct po
{
int nxt,to,w;
}edge[MAXM];
int head[MAXN],dep[MAXN],num=-1;
inline int read()
{
int x=0,c=1;
char ch=‘ ‘;
while((ch>‘9‘||ch<‘0‘)&&ch!=‘-‘)ch=getchar();
while(ch==‘-‘)c=-1,ch=getchar();
while(ch<=‘9‘&&ch>=‘0‘)x=x
10+ch-‘0‘,ch=getchar();
return xc;
}
inline void add_edge(int from,int to,int w)
{
edge[++num].nxt=head[from];
edge[num].to=to;
edge[num].w=w;
head[from]=num;
}
inline void add(int from,int to,int w)
{
add_edge(from,to,w);
add_edge(to,from,0);
}
inline bool bfs()
{
memset(dep,0,sizeof(dep));
queue q;
while(!q.empty())
q.pop();
q.push(s);
dep[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(re int i=head[u];i!=-1;i=edge[i].nxt)
{
int v=edge[i].to;
if(dep[v]==0&&edge[i].w>0)
{
dep[v]=dep[u]+1;
if(v==t)
return 1;
q.push(v);
}
}
}
return 0;
}
inline int dfs(int u,int dis)
{
if(u==t)
return dis;
int diss=0;
for(re int& i=cur[u];i!=-1;i=edge[i].nxt)
{
int v=edge[i].to;
if(edge[i].w!=0&&dep[v]==dep[u]+1)
{
int check=dfs(v,min(dis,edge[i].w));
if(check>0)
{
dis-=check;
diss+=check;
edge[i].w-=check;
edge[i^1].w+=check;
if(dis==0) break;
}
}
}
return diss;
}
inline int dinic()
{
int ans=0;
while(bfs())
{
for(re int i=0;i<=t;i++)
cur[i]=head[i];
while(int d=dfs(s,inf))
ans+=d;
}
return ans;
}
inline int pd1(int x,int y)
{
int ans=0;
if(x!=1) ans+=a1[x-1][y];
if(x!=n) ans+=a1[x][y];
if(y!=1) ans+=a2[x][y-1];
if(y!=m) ans+=a2[x][y];
return ans;
}
inline int pd2(int x,int y)
{
int ans=0;
if(x!=1) ans+=b1[x-1][y];
if(x!=n) ans+=b1[x][y];
if(y!=1) ans+=b2[x][y-1];
if(y!=m) ans+=b2[x][y];
return ans;
}
int main()
{
memset(head,-1,sizeof(head));
n=read();m=read();
s=0;t=n
m+1;
for(re int i=1;i<=n;i++)
for(re int j=1;j<=m;j++)
a[i][j]=read(),id[i][j]=++cnt,tot+=2a[i][j];
for(re int i=1;i<=n;i++)
for(re int j=1;j<=m;j++)
b[i][j]=read(),tot+=2
b[i][j];
for(re int i=1;i<n;i++)
for(re int j=1;j<=m;j++)
a1[i][j]=read(),tot+=a1[i][j]+a1[i][j];
for(re int i=1;i<n;i++)
for(re int j=1;j<=m;j++)
b1[i][j]=read(),tot+=b1[i][j]+b1[i][j];
for(re int i=1;i<=n;i++)
for(re int j=1;j<m;j++)
a2[i][j]=read(),tot+=a2[i][j]+a2[i][j];
for(re int i=1;i<=n;i++)
for(re int j=1;j<m;j++)
b2[i][j]=read(),tot+=b2[i][j]+b2[i][j];
for(re int i=1;i<=n;i++)
for(re int j=1;j<=m;j++){
add(s,id[i][j],2a[i][j]+pd1(i,j));
add(id[i][j],t,2
b[i][j]+pd2(i,j));
if(i!=1) add(id[i][j],id[i-1][j],a1[i-1][j]+b1[i-1][j]);
if(j!=1) add(id[i][j],id[i][j-1],a2[i][j-1]+b2[i][j-1]);
if(i!=n) add(id[i][j],id[i+1][j],a1[i][j]+b1[i][j]);
if(j!=m) add(id[i][j],id[i][j+1],a2[i][j]+b2[i][j]);
}
cout<<(tot-dinic())/2;
return 0;
}

[国家集训队2011]happiness

标签:oid   def   define   ESS   memset   algo   mat   sizeof   push   

原文地址:https://www.cnblogs.com/victorique/p/9084502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!