1. 程式人生 > >BZOJ 1001. BJOI 2006 狼抓兔子

BZOJ 1001. BJOI 2006 狼抓兔子

題目大意

給一個無向圖,其中兩一個點是S,另一個點是T。問最小割。

解題思路

NM106,這張圖網路流能過。
建圖顯然。不過最大的疑點是為何在雙向邊中,正向和反向的兩條邊為何權值一樣。
首先網路流為啥要建反向弧。
如果圖中u點向v點流了x,則建反向弧<v,u>=x,說明如果有更優的流的方案,那麼v可以流x的流量流回u
流的守恆性:整張圖除了源匯點,有多少的流量流向這個點,這個點就有多少流量流出去。
如果uvxvux,說明u沒有流向v
其實本來反向弧是有的,但是進行了下面這麼一波操作。
如果uv有兩條邊,則這兩條邊可以合併成一條,容量為原來兩條邊的容量和。
所以u

v的容量為x的邊和uv的容量為0的邊合併,vu的容量為x的邊和vu的容量為0的邊合併。於是看上去正向和反向的兩條邊為何權值一樣。

程式碼

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 1010
#define Inf 2147483647 
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
struct note{
    int
to,next,val; };note edge[N*N*6]; int tot,head[N*N]; int i,j,n,m,u,v,S,T,x,ans; int pos[N][N]; int qu[N*6010],height[N*N]; int read(){ int fh=1,rs=0;char ch; while((ch<'0'||ch>'9')&&(ch^'-'))ch=getchar(); if(ch=='-')fh=-1,ch=getchar(); while(ch>='0'&&ch<='9')rs=(rs<<3
)+(rs<<1)+(ch^'0'),ch=getchar(); return fh*rs; } void lb(int x,int y,int z){ edge[++tot].to=y;edge[tot].next=head[x];edge[tot].val=z;head[x]=tot; } bool BFS(){ memset(height,-1,sizeof(height)); height[S]=0; int i,j,x,l,r; l=0,r=1; qu[r]=S; while(l<r){ x=qu[++l]; for(i=head[x];i;i=edge[i].next) if(edge[i].val>0&&height[edge[i].to]==-1){ height[edge[i].to]=height[x]+1; qu[++r]=edge[i].to; } } if(height[T]==-1)return 0;else return 1; } int Min(int x,int y){return x<y?x:y;} int find(int u,int thin){ if(u==T)return thin; int a,i,sum=0; for(i=head[u];i;i=edge[i].next) if(height[edge[i].to]==height[u]+1&&edge[i].val>0){ a=find(edge[i].to,Min(edge[i].val,thin)); if(a>0){ sum+=a; thin-=a; edge[i].val-=a; edge[i^1].val+=a; if(!thin)return sum; } } if(!sum)height[u]=-1; return sum; } int main(){ n=read();m=read(); fo(i,1,n)fo(j,1,m)pos[i][j]=++x; S=1,T=n*m; tot=1; fo(i,1,n)fo(j,1,m-1){ x=read(); lb(pos[i][j],pos[i][j+1],x); lb(pos[i][j+1],pos[i][j],x); } fo(i,1,n-1)fo(j,1,m){ x=read(); lb(pos[i][j],pos[i+1][j],x); lb(pos[i+1][j],pos[i][j],x); } fo(i,1,n-1)fo(j,1,m-1){ x=read(); lb(pos[i][j],pos[i+1][j+1],x); lb(pos[i+1][j+1],pos[i][j],x); } ans=0; while(BFS())ans+=find(S,Inf); printf("%d\n",ans); return 0; }