[Bzoj1001] [Wolf catch rabbit], bzoj1001 catch rabbit

Source: Internet
Author: User

[Bzoj1001] [Wolf catch rabbit], bzoj1001 catch rabbit
1001: [BeiJing2006] A wolf catches a rabbit

Time Limit: 15 Sec Memory Limit: 162 MB
Submit: 12719 Solved: 3017
[Submit] [Status] [Discuss]
Description

Now, my friends like the pleasant goat and the Big Wolf most. I can't catch the big wolf, but the rabbits are good. Now, the rabbits are stupid. They only have two nests, now you are the wolf king, facing the following grid terrain:

(1, 1) in the upper left corner, (N, M) in the lower right corner (N = 4, M = 5 ). there are three types of roads: (x, y) <==> (x + 1, y) 2 :( x, y) <==> (x, y + 1) 3: (x, y) <=> (x + 1, y + 1) the weights on the road indicate the maximum number of rabbits that can pass through the road, and the road is undirected. the upper left corner and lower right corner are the rabbit's two nests. At the beginning, all the rabbits are gathered in the upper left corner (). Now they are going to the lower right corner (N, M) of the nest, wolf began to ambush the rabbits. of course, for the sake of insurance, if the number of rabbits allowed to pass a road is K, the wolf king needs to arrange the same number of K wolves to completely block the road, you need to help the wolf king arrange an ambush scheme so that the number of involved wolves is minimized while the rabbit is exhausted. Because the wolf is still in trouble looking for pleasant goat.

Input

First Act N, M. the size of the Grid. N and M are smaller than or equal to 1000. the next part is divided into N rows in the first part, the number of M-1 in each row, indicating the weight of the horizontal road. the second part of the total N-1 line, each line M number, indicating the right of the longitudinal road. the third part of the total N-1 line, the number of M-1 per line, indicating the weight of oblique Road. input File cannot exceed 10 MB

Output

Output an integer to indicate the minimum number of wolves involved in the ambush.

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

Looking at the paper, I learned the plan and dual diagram, "Polar communication-analysis of the application of the maximum and minimum theorem in the informatics competition".
In fact, this plan is converted into a dual chart. In this dual chart, the minimum cut of the original plan is to find the maximum short-circuit length.

#include<iostream>#include<cstdio>#include<cstring>#define T n*(m-1)+(n-1)*m+(n-1)*(m-1)-n*m+3using namespace std;const int N=2001000;int n,m,point[N],next[N*3],tot=1,ans,dis[N],l[10000000];struct S{    int st,en,va;}aa[N*3];bool f[N];inline void add(int x,int y,int z){    tot+=1,next[tot]=point[x];point[x]=tot;    aa[tot].st=x;aa[tot].en=y;aa[tot].va=z;    tot+=1,next[tot]=point[y];point[y]=tot;    aa[tot].st=y;aa[tot].en=x;aa[tot].va=z;}inline int SPFA(int x,int y){    int h=1,t=1,u,i;    memset(dis,127/3,sizeof(dis));    memset(f,1,sizeof(f));    l[h]=x;dis[x]=0;    while(h<=t){        u=l[h];        f[u]=true;        for(i=point[u];i;i=next[i])          if(dis[aa[i].en]>dis[u]+aa[i].va){            dis[aa[i].en]=dis[u]+aa[i].va;            if(f[aa[i].en]){                f[aa[i].en]=false;                t+=1;                l[t]=aa[i].en;            }          }        h+=1;    }    return dis[y];}int main(){    int i,j,x,y,z;    scanf("%d%d",&n,&m);    ans=210000000;    for(i=1;i<=n;++i)      for(j=1;j<m;++j){        scanf("%d",&x);        if(n==1) ans=min(ans,x);        if(i==1) add(1,j*2+1,x);        else if(i==n) add((i-2)*(m-1)*2+j*2,T,x);        else add((i-2)*(m-1)*2+j*2,(i-1)*(m-1)*2+j*2+1,x);      }    for(i=1;i<n;++i)      for(j=1;j<=m;++j){        scanf("%d",&x);        if(m==1) ans=min(ans,x);        if(j==1) add((i-1)*(m-1)*2+2,T,x);        else if(j==m) add(1,i*(m-1)*2+1,x);        else add((i-1)*(m-1)*2+(j-1)*2+1,(i-1)*(m-1)*2+j*2,x);      }    for(i=1;i<n;++i)      for(j=1;j<m;++j){        scanf("%d",&x);        add((i-1)*(m-1)*2+j*2,(i-1)*(m-1)*2+j*2+1,x);      }    if(n==1&&m==1) ans=0;    if(n==1||m==1) printf("%d\n",ans);    else printf("%d\n",SPFA(1,T));}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.