[National Training Team 2011]happiness

Source: Internet
Author: User

pragma comment (linker, "/stack:1024000000,1024000000") include Include Include Include Includeinclude Define 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;
}

[National Training Team 2011]happiness

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.