BZOJ1001: [BeiJing2006]狼抓兔子

1001: [BeiJing2006]狼抓兔子

Time Limit: 15 Sec   Memory Limit: 162 MB
Submit: 19273   Solved: 4745
[ Submit][ Status][ Discuss]

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

一道网络流模板题,因为数据较水可以直接建边跑一遍最大流。
看网上有一种神奇的建边方法,身为蒟蒻的我并没有看懂。
看懂了再来完善。


代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
const int N=1000001;
struct node{
int x,y,z,next,other;
}sa[N*6];int len=0,first[N];
void ins(int x,int y,int z)
{
len++;
sa[len].x=x;
sa[len].y=y;
sa[len].z=z;
sa[len].next=first[x];
first[x]=len;
sa[len].other=len+1;

len++;
sa[len].x=y;
sa[len].y=x;
sa[len].z=z;
sa[len].next=first[y];
first[y]=len;
sa[len].other=len-1;
}
int n,m,st,ed;
int h[N],q[N];
bool bfs()
{
memset(h,0,sizeof(h));
h[st]=1;
int head=1,tail=2;q[1]=st;
while(head!=tail)
{
int x=q[head];
for(int i=first[x];i!=-1;i=sa[i].next)
{
int y=sa[i].y;
if(h[y]==0&&sa[i].z>0)
{
h[y]=h[x]+1;
q[tail]=y;
tail++;
if(tail>N) tail=1;
}
}
head++;
if(head>N) head=1;
}
if(h[ed]==0) return false;
return true;
}
int mymin(int x,int y)
{
if(x<y) return x;
return y;
}
int findans(int x,int f)
{
if(x==ed) return f;
int o,s=0;
for(int i=first[x];i!=-1;i=sa[i].next)
{
int y=sa[i].y;
if(h[x]+1==h[y]&&sa[i].z>0&&s<f)
{
o=findans(y,mymin(f-s,sa[i].z));
s+=o;
sa[i].z-=o;
sa[sa[i].other].z+=o;
}
}
if(s==0) h[x]=0;
return s;
}
int main()
{
scanf("%d%d",&n,&m);
if(n==1&&m==1) {printf("0");return 0;}
len=0;memset(first,-1,sizeof(first));
for(int i=1;i<=n;i++)
    {
        for(int j=2;j<=m;j++)
        {
            int x;scanf("%d",&x);
            ins((i-1)*m+j-1,(i-1)*m+j,x);
        }
    }
    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int x;scanf("%d",&x);
            ins((i-2)*m+j,(i-1)*m+j,x);
        }
    }
    for(int i=2;i<=n;i++)
    {
        for(int j=2;j<=m;j++)
        {
            int x;scanf("%d",&x);
            ins((i-2)*m+j-1,(i-1)*m+j,x);
        }
    }
st=1;ed=n*m;
int ans=0;
while(bfs()==true)
{
ans+=findans(st,99999999);
}
printf("%d",ans);
}
一道网络流模板题
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值