标签:pac ems logs har blog names [] sample mem
Input
Output
Sample Input
3 4 13 2 4 13 7 13 8 12 18 17 0 5 8 13 15 4 11 3 8 11 11 18 6 5 1 2 3 4 4 2 3 2 3 1 0 4 3 2 3 2 0 2 2 1 0 2 4 4
Sample Output
152
Hint
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int iter[500001],que[500001],hh,tt,cap[500001],nxt[500001],to[500001],g[1000001],level[500001],h[500001],k=1; const int INF=999999999; void ins(int u,int v,int c) { nxt[++k]=h[u];to[k]=v;cap[k]=c;h[u]=k; nxt[++k]=h[v];to[k]=u;cap[k]=0;h[v]=k; } inline int read(){ int x; bool f; char c; for (f=0; (c=getchar())<‘0‘||c>‘9‘; f=c==‘-‘); for (x=c-‘0‘; (c=getchar())>=‘0‘&&c<=‘9‘; x=(x<<3)+(x<<1)+c-‘0‘); return f?-x:x; } bool bfs(int S,int T) { memset(level,0,sizeof(level)); hh=tt=0;que[tt++]=S;level[S]=1; while(hh<tt) { int u=que[hh++]; for(int i=h[u];i;i=nxt[i]) { int v=to[i]; if(cap[i]&&!level[v]) { level[v]=level[u]+1; que[tt++]=v; } } } if(!level[T])return 0; return 1; } int dfs(int u,int f,int T) { if(u==T)return f; int used=0,w; for(int &i=iter[u];i;i=nxt[i]) { int v=to[i]; if(cap[i]&&level[v]==level[u]+1) { w=f-used;w=dfs(v,min(w,cap[i]),T); if(w) { cap[i]-=w;cap[i^1]+=w;used+=w;if(used==f)return used; } } } return used; } int dinic(int S,int T,int y) { int flow=0; while(bfs(S,T)) { for(int i=1;i<=y;i++)iter[i]=h[i]; flow+=dfs(S,INF,T); } return flow; } int main() {int tot=0; int n=read(),m=read();int q=n*m,S=q+1,T=q+2,kk=q+2; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { int f=read(),r=(i-1)*m+j;tot+=f; ins(S,r,f); } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { int f=read(),r=(i-1)*m+j;tot+=f; ins(r,T,f); } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { int f=read(),r=(i-1)*m+j;++kk;tot+=f; if(i!=n)ins(kk,r+m,INF); if(i!=1)ins(kk,r-m,INF); if(j!=1)ins(kk,r-1,INF); if(j!=m)ins(kk,r+1,INF); ins(S,kk,f);ins(kk,r,INF); } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { int f=read(),r=(i-1)*m+j;++kk;tot+=f; if(i!=n)ins(r+m,kk,INF); if(i!=1)ins(r-m,kk,INF); if(j!=1)ins(r-1,kk,INF); if(j!=m)ins(r+1,kk,INF); ins(kk,T,f);ins(r,kk,INF); } printf("%d",tot-dinic(S,T,kk)); return 0; }
标签:pac ems logs har blog names [] sample mem
原文地址:http://www.cnblogs.com/lher/p/7598594.html