bzoj1001 [BeiJing2006]狼抓兔子

Description

现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,
而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形:

 

左上角点为(1,1),右下角点为(N,M)(上图中N=4,M=5).有以下三种类型的道路 
1:(x,y)<==>(x+1,y) 
2:(x,y)<==>(x,y+1) 
3:(x,y)<==>(x+1,y+1) 
道路上的权值表示这条路上最多能够通过的兔子数,道路是无向的. 左上角和右下角为兔子的两个窝,
开始时所有的兔子都聚集在左上角(1,1)的窝里,现在它们要跑到右下解(N,M)的窝中去,狼王开始伏击
这些兔子.当然为了保险起见,如果一条道路上最多通过的兔子数为K,狼王需要安排同样数量的K只狼,
才能完全封锁这条道路,你需要帮助狼王安排一个伏击方案,使得在将兔子一网打尽的前提下,参与的
狼的数量要最小。因为狼还要去找喜羊羊麻烦.

Input

第一行为N,M.表示网格的大小,N,M均小于等于1000.
接下来分三部分
第一部分共N行,每行M-1个数,表示横向道路的权值. 
第二部分共N-1行,每行M个数,表示纵向道路的权值. 
第三部分共N-1行,每行M-1个数,表示斜向道路的权值. 
输入文件保证不超过10M

Output

输出一个整数,表示参与伏击的狼的最小数量.

Sample Input

3 4
5 6 4
4 3 1
7 5 3
5 6 7 8
8 7 6 5
5 5 5
6 6 6

Sample Output

14

HINT

 2015.4.16新加数据一组,可能会卡掉从前可以过的程序。

正解:最小割。

题面就是要你求最小割。。然后根据最小割最大流定理,直接dinic求最大流就行。不过要加一个剪枝,就是如果当前这个点没办法增广了,那么这个的点层次就赋为-1。其实真正的正解是平面图转对偶图求最短路,不过懒得写了。。

 

//It is made by wfj_2048~
#include <algorithm>
#include <iostream>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define inf (1<<30)
#define N (1000000)
#define il inline
#define RG register
#define ll long long
#define c(i,j) ( (i-1)*m+j )
#define min(a,b) ( a<b ? a : b )
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)

using namespace std;

struct edge{ int nt,to,flow,cap; }g[6*N+10];

int head[N+10],d[N+10],q[N+10],n,m,num=1;

il int gi(){
    RG int x=0,q=1; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
    if (ch=='-') q=-1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q*x;
}

il void insert(RG int from,RG int to,RG int cap){ g[++num]=(edge){head[from],to,0,cap},head[from]=num; return; }

il int bfs(RG int S,RG int T){
    memset(d,0,sizeof(d));
    RG int h=0,t=1; q[t]=S,d[S]=1;
    while (h<t){
    RG int x=q[++h];
    for (RG int i=head[x];i;i=g[i].nt){
        RG int v=g[i].to;
        if (!d[v] && g[i].cap>g[i].flow){
        q[++t]=v,d[v]=d[x]+1;
        if (v==T) return 1;
        }
    }
    }
    return 0;
}

il int dfs(RG int x,RG int T,RG int a){
    if (x==T || !a) return a; RG int f,flow=0;
    for (RG int i=head[x];i;i=g[i].nt){
    RG int v=g[i].to;
    if (d[v]==d[x]+1 && g[i].cap>g[i].flow){
        f=dfs(v,T,min(a,g[i].cap-g[i].flow));
        if (!f){ d[v]=-1; continue; }
        g[i].flow+=f,g[i^1].flow-=f;
        flow+=f,a-=f; if (!a) return flow;
    }
    }
    return flow;
}

il int maxflow(RG int S,RG int T){ RG int res=0; while (bfs(S,T)) res+=dfs(S,T,inf); return res; }

il void work(){
    n=gi(),m=gi(); RG int x;
    for (RG int i=1;i<=n;++i)
    for (RG int j=1;j<m;++j)
        x=gi(),insert(c(i,j),c(i,j+1),x),insert(c(i,j+1),c(i,j),x);
    for (RG int i=1;i<n;++i)
    for (RG int j=1;j<=m;++j)
        x=gi(),insert(c(i,j),c(i+1,j),x),insert(c(i+1,j),c(i,j),x);
    for (RG int i=1;i<n;++i)
    for (RG int j=1;j<m;++j)
        x=gi(),insert(c(i,j),c(i+1,j+1),x),insert(c(i+1,j+1),c(i,j),x);
    printf("%d\n",maxflow(1,n*m)); return;
}

int main(){
    File("wolf");
    work();
    return 0;
}

 

posted @ 2017-02-22 16:57  wfj_2048  阅读(263)  评论(1编辑  收藏  举报