[國家集訓隊2011]happiness

來源:互聯網
上載者:User

標籤:oid   def   define   ESS   memset   algo   mat   sizeof   push   

pragma comment(linker, "/STACK:1024000000,1024000000")includeincludeincludeincludeincludeincludedefine re registerdefine inf 400000000define MAXN 10005define 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.