标签:
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1001
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<string> 6 #include<algorithm> 7 int next[7000005],first[7000005],go[7000005],tot,val[7000005]; 8 int dis[2000005],c[5000005],S,T,n,m,x,ans=0; 9 10 int sta(int x,int y){ 11 return (x-1)*m+y; 12 } 13 void insert(int x,int y,int z){ 14 tot++; 15 go[tot]=y; 16 next[tot]=first[x]; 17 first[x]=tot; 18 val[tot]=z; 19 20 tot++; 21 go[tot]=x; 22 next[tot]=first[y]; 23 first[y]=tot; 24 val[tot]=z; 25 } 26 bool bfs(){ 27 int now,i; 28 memset(dis,-1,sizeof dis); 29 int h=0,t=1; 30 c[1]=S; 31 dis[S]=0; 32 while (h<t){ 33 h++; 34 now=c[h]; 35 for (int i=first[now];i;i=next[i]){ 36 int pur=go[i]; 37 if (val[i]&&dis[pur]<0){ 38 c[++t]=pur; 39 dis[pur]=dis[now]+1; 40 } 41 } 42 } 43 if (dis[T]==-1) return 0; 44 return 1; 45 } 46 int dfs(int x,int f){ 47 if (x==T) return f; 48 int w,used=0; 49 for (int i=first[x];i;i=next[i]){ 50 int pur=go[i]; 51 if (val[i]&&dis[pur]==dis[x]+1){ 52 w=f-used; 53 w=dfs(pur,std::min(w,val[i])); 54 val[i]-=w; 55 val[i+1]+=w; 56 used+=w; 57 if (used==f) return f; 58 } 59 } 60 if (!used) dis[x]=-1; 61 return used; 62 } 63 void dinic(){ 64 while (bfs()) ans+=dfs(1,0x7fffffff); 65 } 66 int main(){ 67 scanf("%d%d",&n,&m); 68 S=1;T=sta(n,m); 69 for (int i=1;i<=n;i++) 70 for (int j=1;j<=m-1;j++){ 71 scanf("%d",&x); 72 insert(sta(i,j),sta(i,j+1),x); 73 } 74 for (int i=1;i<=n-1;i++) 75 for (int j=1;j<=m;j++){ 76 scanf("%d",&x); 77 insert(sta(i,j),sta(i+1,j),x); 78 } 79 for (int i=1;i<=n-1;i++) 80 for (int j=1;j<=m-1;j++){ 81 scanf("%d",&x); 82 insert(sta(i,j),sta(i+1,j+1),x); 83 } 84 dinic(); 85 printf("%d",ans); 86 }
标签:
原文地址:http://www.cnblogs.com/qzqzgfy/p/5577060.html