题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2127

题解: 这道题就是传说中的“解方程”法。(貌似也有类似于BZOJ 3894的做法,但是边数比较多。)

以下设\(A_i\)\(i\)选文的收益,\(B_i\)\(i\)选理的收益,\(AA_{i,j}\)表示\(i,j\)同文的收益,\(BB_{i,j}\)表示\(i,j\)同理的收益。

首先对于每个点\(i\), 从\(S\)\(i\)\(A_i\),从\(i\)\(T\)\(B_i\)这个没有问题。

然后考虑处理同文同理的代价。

考虑\(S,T,i,j\)\(4\)个点之间可以连\(6\)条边,设\(S\)\(i\), \(i\)\(T\), \(S\)\(j\), \(j\)\(T\), \(i\)\(j\), \(j\)\(i\)的容量分别为\(a,b,c,d,e,f\).

枚举\(i,j\)选文理的四种情况可以列出四个方程。

\(i\in S, j\in S\), 则割掉的边是\(b\)\(d\), 代价是两人不可同理(science)(注意单人文理的代价已经在刚才算过了,所以不要再算!)可得\(b+d=BB_{i,j}\), 同理(reason)可得\(a+c=AA_{i,j}\).

\(i\in S, j\in T\), 则割掉的边是\(b,c,e\) (一定注意没有\(f\)), 代价是二人不可同文或同理(science), 可得\(b+c+e=AA_{i,j}+BB_{i,j}\), 同理(reason)可得\(a+d+f=AA_{i,j}+BB_{i,j}\).

这样我们列出了\(4\)个方程,给\(6\)个变量复制绰绰有余,可以随便取值。但是注意也不能太随便,比如不能出负数等等。一种比较好的取法是: $$a=c=\frac{AA_{i,j}}{2},b=d=\frac{BB_{i,j}}{2},e=f=\frac{AA_{i,j}+BB_{i,j}}{2}$$

最后合并起点终点均相同的边来减少边数,除以\(2\)可以先乘\(2\)再把答案除以\(2\)处理,避免出现小数。

好神仙啊……

代码

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

const int INF = 4e8;

namespace MaxFlow
{
	const int N = 1e4+2;
	const int M = 6e4;
	struct Edge
	{
		int v,w,nxt,rev;
	} e[(M<<1)+3];
	int fe[N+3],te[N+3];
	int que[N+3];
	int dep[N+3];
	int n,en,s,t;
	void addedge(int u,int v,int w)
	{
		en++; e[en].v = v; e[en].w = w;
		e[en].nxt = fe[u]; fe[u] = en; e[en].rev = en+1;
		en++; e[en].v = u; e[en].w = 0;
		e[en].nxt = fe[v]; fe[v] = en; e[en].rev = en-1;
	}
	bool bfs()
	{
		for(int i=1; i<=n; i++) dep[i] = 0;
		int head = 1,tail = 1; que[tail] = s; dep[s] = 1;
		while(head<=tail)
		{
			int u = que[head]; head++;
			for(int i=fe[u]; i; i=e[i].nxt)
			{
				if(dep[e[i].v]==0 && e[i].w>0)
				{
					dep[e[i].v] = dep[u]+1;
					tail++; que[tail] = e[i].v;
				}
			}
		}
		return dep[t]!=0;
	}
	int dfs(int u,int cur)
	{
		if(u==t) return cur;
		int rst = cur;
		for(int i=te[u]; i; i=e[i].nxt)
		{
			if(dep[e[i].v]==dep[u]+1 && rst>0 && e[i].w>0)
			{
				int flow = dfs(e[i].v,min(rst,e[i].w));
				if(flow>0)
				{
					e[i].w -= flow; e[e[i].rev].w += flow; rst -= flow;
					if(e[i].w>0) te[u] = i;
					if(rst==0) return cur;
				}
			}
		}
		if(rst==cur) dep[u] = 0;
		return cur-rst;
	}
	int dinic(int _n,int _s,int _t)
	{
		n = _n,s = _s,t = _t;
		int ret = 0;
		while(bfs())
		{
			for(int i=1; i<=n; i++) te[i] = fe[i];
			ret += dfs(s,INF);
		}
		return ret;
	}
}
using MaxFlow::addedge;
using MaxFlow::dinic;

const int N = 100;
int a[N+3][N+3];
int b[N+3][N+3];
int aa1[N+3][N+3];
int aa2[N+3][N+3];
int bb1[N+3][N+3];
int bb2[N+3][N+3];
int n,m;

int getid(int x,int y) {return (x-1)*m+y+2;}

int main()
{
	scanf("%d%d",&n,&m); int ans = 0;
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=m; j++) scanf("%d",&a[i][j]),ans += a[i][j];
	}
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=m; j++) scanf("%d",&b[i][j]),ans += b[i][j];
	}
	for(int i=1; i<n; i++)
	{
		for(int j=1; j<=m; j++) scanf("%d",&aa1[i][j]),ans += aa1[i][j];
	}
	for(int i=1; i<n; i++)
	{
		for(int j=1; j<=m; j++) scanf("%d",&bb1[i][j]),ans += bb1[i][j];
	}
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<m; j++) scanf("%d",&aa2[i][j]),ans += aa2[i][j];
	}
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<m; j++) scanf("%d",&bb2[i][j]),ans += bb2[i][j];
	}
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=m; j++)
		{
			int x = getid(i,j);
			addedge(1,x,(a[i][j]<<1)+aa1[i][j]+aa1[i-1][j]+aa2[i][j]+aa2[i][j-1]);
			addedge(x,2,(b[i][j]<<1)+bb1[i][j]+bb1[i-1][j]+bb2[i][j]+bb2[i][j-1]);
			if(i<n)
			{
				int y = getid(i+1,j);
				addedge(x,y,aa1[i][j]+bb1[i][j]);
				addedge(y,x,aa1[i][j]+bb1[i][j]);
			}
			if(j<m)
			{
				int y = getid(i,j+1);
				addedge(x,y,aa2[i][j]+bb2[i][j]);
				addedge(y,x,aa2[i][j]+bb2[i][j]);
			}
		}
	}
	int tmp = dinic(n*m+2,1,2);
	ans -= (tmp>>1);
	printf("%d\n",ans);
	return 0;
}