标签:line void dfs gis getch 最小割 fine 建图 return
bzoj打卡2天233~
闲话不说,我们进入题解:
#include<cstdio> #include<cstring> #define inf 0x3f3f3f3f #define r register #define min(a,b) (a<b?a:b) typedef long long ll; inline int read(){ r int x=0,f=1;r char c=getchar(); for(;c<‘0‘||c>‘9‘;f=c==‘-‘?-1:1,c=getchar()); for(;c>=‘0‘&&c<=‘9‘;x=(x<<3)+(x<<1)+c-‘0‘,c=getchar()); return x*f; } struct AriM{int to,nxt,c;}e[2996005*2]; int cnt,head[1000005]; int h[1000005],q[1500005]; inline void ins(int s,int t,int c){ e[++cnt].to=t,e[cnt].nxt=head[s],head[s]=cnt,e[cnt].c=c; e[++cnt].to=s,e[cnt].nxt=head[t],head[t]=cnt,e[cnt].c=c; } int n,m,ans; bool bfs(){ memset(h,-1,sizeof(h)); r int s=0,t=1; q[s]=1;h[1]=0; while(s<t){ r int v=q[s++]; for(r int i=head[v];i;i=e[i].nxt) if(e[i].c&&h[e[i].to]<0){ q[t++]=e[i].to; h[e[i].to]=h[v]+1; } } return h[n*m]!=-1; } int dfs(int v,int f){ if(v==n*m)return f; r int used=0; for(r int i=head[v];i;i=e[i].nxt) if(e[i].c&&h[e[i].to]==h[v]+1){ r int w=f-used; w=dfs(e[i].to,min(w,e[i].c)); e[i].c-=w;e[i+1].c+=w;used+=w; if(used==f)return f; } if(!used)h[v]=-1; return used; } void Dinic(){while(bfs())ans+=dfs(1,inf);} int main(){ n=read();m=read(); for(r int i=1;i<=n;i++) for(r int j=1;j<m;j++) ins(i*m-m+j,i*m-m+j+1,read()); for(r int i=1;i<n;i++) for(r int j=1;j<=m;j++) ins(i*m-m+j,i*m+j,read()); for(r int i=1;i<n;i++) for(r int j=1;j<m;j++) ins(i*m-m+j,i*m+j+1,read()); Dinic(); printf("%d\n",ans); return 0; }
标签:line void dfs gis getch 最小割 fine 建图 return
原文地址:http://www.cnblogs.com/Marser/p/7345350.html