标签:++ ring 获得 oid out str main 必须 top
2
7 7
54 54
61
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int INF=1999999999; int h[1000000],next[1000000],cap[1000000],to[1000000],k=1; int level[40100],que[40100],head,tail,iter[40100],n; int x[4]={-1,0,1,0},y[4]={0,-1,0,1}; void ins(int u,int v,int c){next[++k]=h[u];h[u]=k;to[k]=v;cap[k]=c;} int get(int i,int j){return (i-1)*n+j;} bool bfs(int S,int T) { memset(level,0,sizeof(level)); head=tail=0;que[tail++]=S;level[S]=1; while(head<tail) { int u=que[head++]; for(int i=h[u];i;i=next[i]) { int v=to[i]; if(!level[v]&&cap[i]) { level[v]=level[u]+1;que[tail++]=v; } } } if(!level[T])return 0;return 1; } int dfs(int u,int T,int f) { if(u==T)return f; int used=0; for(int &i=iter[u];i;i=next[i]) { int v=to[i]; if(level[v]==level[u]+1&&cap[i]) { int w=dfs(v,T,min(cap[i],f-used));used+=w; if(w) { cap[i]-=w;cap[i^1]+=w;if(f==used)return f; } } } return used; } int dinic(int S,int T) { int flow=0; while(1) { for(int i=0;i<=n*n+1;i++)iter[i]=h[i]; if(!bfs(S,T))return flow;flow+=dfs(S,T,INF); } } int main() { scanf("%d",&n);int S=0,T=n*n+1,tot=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { int v;scanf("%d",&v);tot+=v; if(i%2==j%2){ins(get(i,j),T,v);ins(T,get(i,j),0);} else { for(int k=0;k<4;k++) { int ii=i+x[k],jj=j+y[k];if(ii<1||ii>n||jj<1||jj>n)continue; ins(get(i,j),get(ii,jj),INF);ins(get(ii,jj),get(i,j),0); } ins(S,get(i,j),v);ins(get(i,j),S,0); } } cout<<tot-dinic(S,T); return 0; }
标签:++ ring 获得 oid out str main 必须 top
原文地址:http://www.cnblogs.com/lher/p/7581417.html