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。第一行,两个整数,分别是n和m(1≤n,m≤100);
任何数字不超过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
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
【数据规模】
对于100%的数据有N,M≤100
【数据规模】
对于100%的数据有N,M≤100
网络流的另一种建模姿势
二元组——>最小割
把图黑白染色,黑格工业连S,商业连T,白格反之。。。
黑白格相互之间连边,流量为c[x][y]+c[nx][ny]
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<iostream> 5 using namespace std; 6 const int inf=100000000,N=110; 7 struct ee{int to,next,f;}e[N*N*N]; 8 int head[N*N],q[N*N],dis[N*N],a[N][N],b[N][N],c[N][N],col[N][N]; 9 int fx[4]={0,0,1,-1},fy[4]={1,-1,0,0}; 10 int S,T=10001,n,m,cnt=1,ans,w,mid,tot; 11 bool mark[N]; 12 void ins(int u,int v,int w) 13 {cnt++;e[cnt].to=v;e[cnt].f=w;e[cnt].next=head[u];head[u]=cnt;} 14 void insert(int u,int v,int w) 15 {ins(u,v,w);ins(v,u,0);} 16 17 bool bfs(){ 18 for (int i=1;i<=T;i++) dis[i]=inf; 19 int h=0,t=1,now; 20 q[1]=S;dis[S]=0; 21 while(h!=t){ 22 now=q[++h]; 23 for (int i=head[now];i;i=e[i].next){ 24 int v=e[i].to; 25 if (e[i].f&&dis[now]+1<dis[v]){ 26 dis[v]=dis[now]+1; 27 if (v==T)return 1; 28 q[++t]=v; 29 } 30 } 31 } 32 if (dis[T]==inf) return 0; return 1; 33 } 34 35 int dinic(int now,int f){ 36 if (now==T) return f; 37 int rest=f; 38 for (int i=head[now];i;i=e[i].next){ 39 int v=e[i].to; 40 if (e[i].f&&dis[v]==dis[now]+1&&rest){ 41 int t=dinic(v,min(rest,e[i].f)); 42 if (!t) dis[v]=0; 43 e[i].f-=t; 44 e[i^1].f+=t; 45 rest-=t; 46 } 47 } 48 return f-rest; 49 } 50 51 void build(){ 52 for(int i=1;i<=n;i++) 53 for(int j=1;j<=m;j++) 54 if(col[i][j]) swap(a[i][j],b[i][j]); 55 for(int i=1;i<=n;i++) 56 for(int j=1;j<=m;j++){ 57 insert(S,(i-1)*m+j,a[i][j]); 58 insert((i-1)*m+j,T,b[i][j]); 59 ans+=a[i][j];ans+=b[i][j]; 60 if(col[i][j]) 61 for(int k=0;k<4;k++){ 62 int nx=i+fx[k],ny=j+fy[k]; 63 if(nx>n||nx<1||ny>m||ny<1) continue; 64 ins((nx-1)*m+ny,(i-1)*m+j,c[i][j]+c[nx][ny]); 65 ins((i-1)*m+j,(nx-1)*m+ny,c[i][j]+c[nx][ny]); 66 ans+=(c[i][j]+c[nx][ny]); 67 } 68 } 69 } 70 71 int main(){ 72 scanf("%d%d",&n,&m); 73 for(int i=1;i<=n;i++) 74 for(int j=1;j<=m;j++) scanf("%d",&a[i][j]); 75 for(int i=1;i<=n;i++) 76 for(int j=1;j<=m;j++) scanf("%d",&b[i][j]); 77 for(int i=1;i<=n;i++) 78 for(int j=1;j<=m;j++) scanf("%d",&c[i][j]); 79 for(int i=1;i<=n;i++) 80 for(int j=1;j<=m;j++) 81 if ((i+j)&1==1) col[i][j]=1; 82 build(); 83 while(bfs()) tot+=dinic(S,inf); 84 printf("%d",ans-tot); 85 }