[bzoj2132][网络流-最小割]圈地计划

xiaoxiao2021-02-28  31

Description

最近房地产商GDOI(Group of Dumbbells Or Idiots)从NOI(Nuts Old Idiots)手中得到了一块开发土地。据了解, 这块土地是一块矩形的区域,可以纵横划分为N×M块小区域。GDOI要求将这些区域分为商业区和工业区来开发。根 据不同的地形环境,每块小区域建造商业区和工业区能取得不同的经济价值。更具体点,对于第i行第j列的区域, 建造商业区将得到Aij收益,建造工业区将得到Bij收益。另外不同的区域连在一起可以得到额外的收益,即如果区 域(I,j)相邻(相邻是指两个格子有公共边)有K块(显然K不超过4)类型不同于(I,j)的区域,则这块区域能增加k ×Cij收益。经过Tiger.S教授的勘察,收益矩阵A,B,C都已经知道了。你能帮GDOI求出一个收益最大的方案么?

Input

输入第一行为两个整数,分别为正整数N和M,分别表示区域的行数和列数; 第2到N+1列,每行M个整数,表示商业区收益矩阵A; 第N+2到2N+1列,每行M个整数,表示工业区收益矩阵B; 第2N+2到3N+1行,每行M个整数,表示相邻额外收益矩阵C。 任何数字不超过1000”的限制

Output

输出只有一行,包含一个整数,为最大收益值。

Sample Input

3 3

1 2 3

4 5 6

7 8 9

9 8 7

6 5 4

3 2 1

1 1 1

1 3 1

1 1 1

Sample Output

81

HINT

【数据规模】

对于100%的数据有N,M≤100

题解

跑朴素最小割的话,我们会发现一个问题 设两个点(i,j),(x,y),处于不同集合,那么他们中间的那条边其实是一定要割去的,也就是C[i][j]+C[x][y]肯定会损失。如果想不损失的话这个玩意要建负权。。 所以我们黑白染色。 相邻的格子连边不同 设白点 st->点连边 流量A[i][j],点->ed连边,流量B[i][j] 黑点相反 那么这样我们可以发现,当黑点与白点同在一个集合时,他们其实是不同的地。。那么中间连的C[i][j]+C[x][y]不会损失,反之亦然

#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; struct node { int x,y,c,next,other; }a[4110000];int len,last[11000]; const int dx[4]={0,-1,0,1}; const int dy[4]={-1,0,1,0}; void ins(int x,int y,int c) { int k1,k2; k1=++len; a[len].x=x;a[len].y=y;a[len].c=c; a[len].next=last[x];last[x]=len; k2=++len; a[len].x=y;a[len].y=x;a[len].c=0; a[len].next=last[y];last[y]=len; a[k1].other=k2;a[k2].other=k1; } int h[11000],list[2500000],head,tail,st,ed; bool bt_h() { memset(h,0,sizeof(h)); head=1;tail=2;list[1]=st;h[st]=1; while(head!=tail) { int x=list[head]; for(int k=last[x];k;k=a[k].next) { int y=a[k].y; if(h[y]==0 && a[k].c>0) { h[y]=h[x]+1; list[tail++]=y; } } head++; } if(h[ed]>0)return true; return false; } int findflow(int x,int f) { if(x==ed)return f; int s=0,t; for(int k=last[x];k;k=a[k].next) { int y=a[k].y; if(h[y]==h[x]+1 && a[k].c>0 && s<f) { s+=(t=findflow(y,min(a[k].c,f-s))); a[k].c-=t;a[a[k].other].c+=t; } } if(s==0)h[x]=0; return s; } int n,m; int A[110][110],B[110][110],C[110][110]; int col[110][110]; int pt(int x,int y){return (x-1)*m+y;} int main() { scanf("%d%d",&n,&m); int 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",&B[i][j]),sum+=B[i][j]; for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) { scanf("%d",&C[i][j]); for(int k=0;k<=3;k++)if(i+dx[k]>=1 && i+dx[k]<=n && j+dy[k]>=1 && j+dy[k]<=m)sum+=C[i][j]; } st=n*m+1;ed=n*m+2; memset(col,0,sizeof(col)); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { if(i==1 && j==1)continue; if(j==1)col[i][j]=col[i-1][j]^1; else col[i][j]=col[i][j-1]^1; } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { if(col[i][j]==0)ins(st,pt(i,j),A[i][j]),ins(pt(i,j),ed,B[i][j]); else ins(st,pt(i,j),B[i][j]),ins(pt(i,j),ed,A[i][j]); } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) for(int k=0;k<=3;k++) { int x=i+dx[k],y=j+dy[k]; if(x<1 || x>n || y<1 || y>m)continue; ins(pt(i,j),pt(x,y),C[i][j]+C[x][y]); } int ans=0; while(bt_h())ans+=findflow(st,999999999); printf("%d\n",sum-ans); return 0; }
转载请注明原文地址: https://www.6miu.com/read-2627514.html

最新回复(0)