#include <stdio.h> int main() { puts("转载请注明出处谢谢"); puts("http://blog.csdn.net/vmurder/article/details/43201521"); }
题解:
水题,经典模型是两个在一块会损失,显然很好做。
这个同样很好做,就是黑白染色,然后某种颜色该连S集的连T,该连T的连S。
代码:
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 10100 #define M 200000 #define P 105 #define inf 0x3f3f3f3f using namespace std; const int dx[]={0,0,1,-1}; const int dy[]={1,-1,0,0}; struct KSD { int v,len,next; }e[M]; int head[N],cnt; inline void add(int u,int v,int len) { e[++cnt].v=v; e[cnt].len=len; e[cnt].next=head[u]; head[u]=cnt; } int s,t,d[N]; queue<int>q; bool bfs() { while(!q.empty())q.pop(); memset(d,0,sizeof d); q.push(s),d[s]=1; int i,u,v; while(!q.empty()) { u=q.front(),q.pop(); for(i=head[u];i;i=e[i].next) { v=e[i].v; if(!d[v=e[i].v]&&e[i].len) { d[v]=d[u]+1; if(v==t)return true; q.push(v); } } } return false; } int dinic(int x,int flow) { if(x==t)return flow; int remain=flow,i,v,k; for(i=head[x];i&&remain;i=e[i].next) { if(d[v=e[i].v]==d[x]+1&&e[i].len) { k=dinic(v,min(remain,e[i].len)); if(!k)d[v]=0; e[i].len-=k,e[i^1].len+=k; remain-=k; } } return flow-remain; } int n,m,maxflow; int map1[P][P],map2[P][P],map3[P][P]; int id[P][P]; void build() { int i,j,k; int x,y; scanf("%d%d",&n,&m); for(i=1;i<=n;i++)for(j=1;j<=m;j++)scanf("%d",&map1[i][j]); for(i=1;i<=n;i++)for(j=1;j<=m;j++)scanf("%d",&map2[i][j]); for(i=1;i<=n;i++)for(j=1;j<=m;j++)scanf("%d",&map3[i][j]); for(i=1;i<=n;i++)for(j=1;j<=m;j++)id[i][j]=++cnt; s=cnt+1,t=cnt+2,cnt=1; for(i=1;i<=n;i++)for(j=1;j<=m;j++) { if(i+j&1) { add(s,id[i][j],map1[i][j]); add(id[i][j],s,map1[i][j]); add(id[i][j],t,map2[i][j]); add(t,id[i][j],map2[i][j]); } else { add(s,id[i][j],map2[i][j]); add(id[i][j],s,map2[i][j]); add(id[i][j],t,map1[i][j]); add(t,id[i][j],map1[i][j]); } maxflow+=map1[i][j]+map2[i][j]; for(k=0;k<4;k++) { x=i+dx[k]; y=j+dy[k]; if(id[x][y]) { add(id[i][j],id[x][y],map3[i][j]); add(id[x][y],id[i][j],map3[i][j]); maxflow+=map3[i][j]; } } } } int main() { // freopen("test.in","r",stdin); build(); while(bfs())maxflow-=dinic(s,inf); printf("%d\n",maxflow); return 0; }
原文地址:http://blog.csdn.net/vmurder/article/details/43201521