zoukankan      html  css  js  c++  java
  • 1001: [BeiJing2006]狼抓兔子

    1001: [BeiJing2006]狼抓兔子

    Time Limit: 15 Sec  Memory Limit: 162 MB
    Submit: 12827  Solved: 3044
    [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
     
    题解:

      以1为源点,以m*n为汇点用Dinic跑一遍最大流即可,其中要注意由于题目中说了是无向边所以加边的时候要注意正向反向的边的权值要一样。

      写dinic()的BFS()时要整体跑完dis[]才能退出,如果只是发现能连向T就跳出,会造成有点的dis[]没有计算。还有一个有效的优化,就是在DFS()时如果结束时now==0,说明这个节点对答案不会再有贡献,直接dis[i]=0。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cmath>
     5 #include<cstring>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<vector>
     9 using namespace std;
    10 const int inf=1e9;
    11 const int maxn=1000001,maxm=6000001;
    12 inline int read(){
    13     int x=0,f=1;char ch=getchar();
    14     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    15     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    16     return x*f;
    17 }
    18 int N,M,val,S,T;
    19 struct node{
    20     int to,next,rest;
    21 }e[maxm];
    22 int head[maxn],ecnt=1;
    23 inline void addedge(int x,int y,int r){
    24     e[++ecnt].to=y; e[ecnt].rest=r; e[ecnt].next=head[x]; head[x]=ecnt;
    25     e[++ecnt].to=x; e[ecnt].rest=r; e[ecnt].next=head[y]; head[y]=ecnt;
    26 }
    27 int dis[maxn];
    28 bool BFS(){
    29     for(int i=S;i<=T;i++) dis[i]=0;
    30     dis[S]=1;
    31     static queue<int> Q;
    32     Q.push(S);
    33     while(!Q.empty()){
    34         int x=Q.front(); Q.pop();
    35         for(int i=head[x];i;i=e[i].next){
    36             int y=e[i].to;
    37             if(e[i].rest&&dis[y]==0){
    38                 dis[y]=dis[x]+1;
    39                 Q.push(y);
    40             }
    41         }
    42     }
    43     if(dis[T]) return true;
    44     return false;
    45 }
    46 int DFS(int x,int flow){
    47     if(x==T) return flow;
    48     int now=0,tmp;
    49     for(int i=head[x];i;i=e[i].next){
    50         int y=e[i].to;
    51         if(e[i].rest&&dis[y]==dis[x]+1){
    52             tmp=DFS(y,min(flow-now,e[i].rest));
    53             e[i].rest-=tmp;
    54             e[i^1].rest+=tmp;
    55             now+=tmp;
    56             if(flow==now) return now;
    57         }
    58     }
    59     if(!now) dis[x]=0;
    60     return now;
    61 }
    62 int dinic(){
    63     int ans=0;
    64     while(BFS()){
    65         ans+=DFS(S,inf);
    66     }
    67     return ans;
    68 }
    69 int main(){
    70     N=read(); M=read();
    71     S=0; T=N*M+1;
    72     addedge(S,1,inf); addedge(N*M,T,inf);
    73     for(int i=1;i<=N;i++){
    74         for(int j=1;j<=M-1;j++){
    75             val=read();
    76             addedge((i-1)*M+j,(i-1)*M+j+1,val);    
    77         } 
    78     }
    79     for(int i=1;i<=N-1;i++){
    80         for(int j=1;j<=M;j++){
    81             val=read();
    82             addedge((i-1)*M+j,i*M+j,val);
    83         }
    84     }
    85     for(int i=1;i<=N-1;i++){
    86         for(int j=1;j<=M-1;j++){
    87             val=read();
    88             addedge((i-1)*M+j,i*M+j+1,val);
    89         }
    90     }
    91     printf("%d",dinic());
    92     return 0;
    93 }
  • 相关阅读:
    [leetcode]687. Longest Univalue Path
    [leetcode]543. Diameter of Binary Tree二叉树的直径
    [LeetCode]Subtree of Another Tree判断一棵树是不是另一棵树的子树
    [leetcode]508. Most Frequent Subtree Sum二叉树中出现最多的值
    [leetcode]450. Delete Node in a BST二叉搜索树删除节点
    [LeetCode]652. Find Duplicate Subtrees找到重复树
    MySQL 数据库
    javaScript
    Css 笔记
    Html 笔记
  • 原文地址:https://www.cnblogs.com/CXCXCXC/p/4645092.html
Copyright © 2011-2022 走看看