bzoj2127: happiness
跟上题(文理分科)几乎一样啊。
结果findflow没有h[x]==0 T了N次。。。
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; const int inf=999999999; int n,m; struct node { int x,y,c,next,other; }a[5100000];int len,last[2100000]; void ins(int x,int y,int c) { int k1,k2; len++;k1=len; a[len].x=x;a[len].y=y;a[len].c=c; a[len].next=last[x];last[x]=len; len++;k2=len; a[len].x=y;a[len].y=x;a[len].c=0; a[len].next=last[y];last[y]=len; a[k1].other=k2; a[k2].other=k1; } int st,ed,h[2100000],list[2100000]; bool bt_h() { memset(h,0,sizeof(h));h[st]=1; int head=1,tail=2;list[1]=st; while(head!=tail) { int x=list[head]; for(int k=last[x];k;k=a[k].next) { int y=a[k].y; if(h[y]==0&&a[k].c>0) { h[y]=h[x]+1; list[tail]=y; tail++; } } head++; } if(h[ed]==0)return false; return true; } int findflow(int x,int f) { if(x==ed)return f; int s=0; for(int k=last[x];k;k=a[k].next) { int y=a[k].y; if(h[y]==h[x]+1&&a[k].c>0&&s<f) { int t=findflow(y,min(a[k].c,f-s)); s+=t;a[k].c-=t;a[a[k].other].c+=t; } } if(s==0)h[x]=0; return s; } //-----------wang luo liu------------- int sum; int point(int x,int y){return (x-1)*m+y;} void compostion() { st=n*m+1,ed=n*m+2; int x; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&x), ins(st,point(i,j),x), sum+=x; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&x), ins(point(i,j),ed,x), sum+=x; int cnt=ed; for(int i=1;i<n;i++) for(int j=1;j<=m;j++) { scanf("%d",&x); cnt++; ins(st,cnt,x); ins(cnt,point(i,j),inf); ins(cnt,point(i+1,j),inf); sum+=x; } for(int i=1;i<n;i++) for(int j=1;j<=m;j++) { scanf("%d",&x); cnt++; ins(cnt,ed,x); ins(point(i,j),cnt,inf); ins(point(i+1,j),cnt,inf); sum+=x; } for(int i=1;i<=n;i++) for(int j=1;j<m;j++) { scanf("%d",&x); cnt++; ins(st,cnt,x); ins(cnt,point(i,j),inf); ins(cnt,point(i,j+1),inf); sum+=x; } for(int i=1;i<=n;i++) for(int j=1;j<m;j++) { scanf("%d",&x); cnt++; ins(cnt,ed,x); ins(point(i,j),cnt,inf); ins(point(i,j+1),cnt,inf); sum+=x; } } int main() { scanf("%d%d",&n,&m); sum=0; compostion(); int ans=0; while(bt_h()==true) { ans+=findflow(st,inf); } printf("%d\n",sum-ans); return 0; }
pain and happy in the cruel world.