题意:https://www.lydsy.com/JudgeOnline/problem.php?id=3894
思路:最小割的模型,最小割解决的其实是最小的合法方案。而这题要求最大,所以我们将问题转化为减法。
我们考虑这样一个图
原点 --边a---> 点B -边b-----> 汇点
那么我们会发现最后的最小割一定会在在边a和边b做出一个较小的选择
接下来我们引入二元关系
我们会发现,最后的最小割只会有3种情况
ax + ay
bx + by
ax + v + by
ay + v + bx
(手画一下就能发现)
那么这就刚好对应于 这道题的模型。
链接:https://www.cnblogs.com/chenyushuo/p/5146626.html
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef LL lint;
const lint inf = 0x3f3f3f3f3f3f;
const int maxn = 200;
const int maxm = 200;
struct dinic{
static const int N = 1000005;
static const int M = 20000005;
static const lint inf = 0x3f3f3f3f3f3f;
int he[N],ne[M],ver[M],d[N];
lint edge[M];
int s,t,tot;
queue<int> que;
void init( int n ){
for( int i = 0;i <= n;i++ ){
he[i] = 0;
}
tot = 1;
}
void add( int x,int y,lint z ){
ver[++tot] = y; ne[ tot ] = he[x]; he[x] = tot; edge[ tot ] = z;
ver[++tot] = x; ne[tot] = he[y]; he[y] = tot;edge[tot] = 0;
}
bool bfs(){
memset( d,0,sizeof( d ) );
while( que.size() ) que.pop();
que.push( s );
d[s] = 1;
while( que.size() ){
int x = que.front();
que.pop();
for( int cure = he[x];cure;cure = ne[cure] ){
int y = ver[cure];
if( edge[cure] && !d[y] ){
que.push( y );
d[ y ] = d[x] + 1;
if( y == t ) return 1;
}
}
}
return 0;
}
lint dfs( int x,lint flow ){
if( x== t ) return flow;
lint rest = flow,k;
for( int cure = he[x];cure && rest;cure = ne[cure] ){
int y = ver[cure];
if( edge[cure] && d[ y ] == d[x] + 1 ){
k = dfs( y,min( rest,edge[cure] ) );
if( !k ) d[ y ] = 0;
edge[cure] -= k;
edge[ cure^1 ] += k;
rest -= k;
}
}
return flow - rest;
}
lint max_flow( int x,int y ){
s = x; t = y;
lint maxflow = 0;
lint flow = 0;
while( bfs() )
while( flow = dfs( s,inf ) ) maxflow += flow;
return maxflow;
}
} g;
int a[maxn][maxm],s[maxn][maxm],ss[maxn][maxm],sa[maxn][maxm],n,m;
int cal_1( int x,int y ){
if( x <= 0 || y <= 0 || x>n || y > m ) return -1;
return (x-1)*m + y;
}
int cal_2( int x,int y ){
if( x <= 0 || y <= 0 || x> n || y > m ) return -1;
return ( x-1 )*m + y + m*n;
}
int cal_3( int x,int y ){
if( x<=0 || y <= 0 || x > n|| y > m ) return -1;
return (x-1)*m + y +2*m*n;
}
int main(){
int dir[5][2] = { { 0,0 },{ 1,0 },{ -1,0 },{0,1},{0,-1} };
scanf("%d%d",&n,&m);
LL sum = 0;
for( int i = 1;i <= n;i++ )for( int j = 1;j <= m;j++ )scanf("%d",&a[i][j]),sum += a[i][j];
for( int i = 1;i <= n;i++ )for( int j = 1;j <= m;j++ )scanf("%d",&s[i][j]),sum += s[i][j];
for( int i = 1;i <= n;i++ )for( int j = 1;j <= m;j++ ) scanf("%d",&sa[i][j]),sum += sa[i][j];
for( int i = 1;i <= n;i++ )for( int j = 1;j <= m;j++ )scanf("%d",&ss[i][j]),sum +=ss[i][j];
int S = 0,T = 3*n*m+1;
g.init(T);
for( int i = 1;i <= n;i++ ){
for( int j = 1;j <= m;j++ ){
int id1 = cal_1( i,j );
g.add( S,id1,a[i][j] );
g.add( id1,T,s[i][j] );
}
}
for( int i = 1;i <= n;i++ ){
for( int j = 1;j <= m;j++ ){
int id2 = cal_2( i,j );
int id3 = cal_3( i,j );
g.add( S,id3,sa[i][j] );
g.add( id2,T,ss[i][j] );
for( int k = 0;k < 5;k++ ){
int ii = i+dir[k][0];
int jj = j+dir[k][1];
int id1 =cal_1( ii,jj );
if( id1==-1 ) continue;
g.add( id3,id1,inf );
g.add( id1,id2,inf );
}
}
}
sum -= g.max_flow( S,T );
printf("%lld\n",sum);
return 0;
}