zoukankan      html  css  js  c++  java
  • BZOJ1001:狼抓兔子(最小割最大流+vector模板)

    1001: [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算法,其精髓就是dfs可多条路同时增广,一开始用vector谜之爆内存,所以改为前向星。

    注意一下当前弧优化,很有用的一个优化。

    代码如下:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <vector>
    #include <queue>
    #define INF 0x3f3f3f
    using namespace std;
    
    const int N = 1000005,M = 6000005;
    struct Edge{
        int v,c,next;
    }e[M];
    int head[N],d[N],cur[N];
    int n,m,tot=0;
    void adde(int u,int v,int w){
        e[tot].v=v;e[tot].c=w;e[tot].next=head[u];head[u]=tot++;
        e[tot].v=u;e[tot].c=w;e[tot].next=head[v];head[v]=tot++;
    }
    bool bfs(int s,int t){
        queue<int> q;
        memset(d,0,sizeof(d));d[s]=1;
        q.push(s);
        while(!q.empty()){
            int u=q.front();q.pop();
            for(int i=head[u];i!=-1;i=e[i].next){
                int v=e[i].v;
                if(!d[v] && e[i].c>0){
                    d[v]=d[u]+1;
                    q.push(v);
                }
            }
        }
        return d[t]!=0;
    }
    int dfs(int s,int a){
        if(s==n*m || a==0) return a;
        int flow=0,f;
        for(int &i=cur[s];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(d[v]!=d[s]+1) continue ;
            f=dfs(v,min(a,e[i].c));
            if(f>0){
                e[i].c-=f;
                e[i^1].c+=f;
                flow+=f;
                a-=f;
                if(a==0) break;
            }
        }
        if(!flow) d[s]=-1;
        return flow;
    }
    int Dinic(){
        int ans = 0;
        while(bfs(1,n*m)){
            for(int i=1;i<=n*m;i++) cur[i]=head[i];
            ans+=dfs(1,INF);
        }
        return ans;
    }
    int main(){
        scanf("%d%d",&n,&m);
        int w;
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m-1;j++){
                scanf("%d",&w);
                adde(m*(i-1)+j,m*(i-1)+j+1,w);
            }
        }
        for(int i=1;i<=n-1;i++){
            for(int j=1;j<=m;j++){
                scanf("%d",&w);
                adde(j+(i-1)*m,j+i*m,w);
            }
        }
        for(int i=1;i<=n-1;i++){
            for(int j=1;j<=m-1;j++){
                scanf("%d",&w);
                adde((i-1)*m+j,i*m+j+1,w);
            }
        }
        cout<<Dinic();
        return 0;
    }
     

    vector写了也不能白写,附上模板吧...

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <vector>
    #include <queue>
    #define INF 0x3f3f3f
    using namespace std;
    
    const int N = 1000005;
    
    struct Edge{
        int u,v,flow,c;
    };
    
    vector <int > vec[N];
    vector <Edge> edges;
    int n,m;
    int cur[N],d[N];
    
    void adde(int u,int v,int w){
        edges.push_back((Edge){u,v,0,w});
        edges.push_back((Edge){v,u,0,w});
        int m = edges.size();
        vec[u].push_back(m-2);
        vec[v].push_back(m-1);
    }
    bool bfs(int s,int t){
        memset(d,0,sizeof(d));d[s]=1;
        queue <int> q;
        q.push(s);
        while(!q.empty()){
            int u = q.front();q.pop();
            for(int i=0;i<vec[u].size();i++){
                Edge& E = edges[vec[u][i]];
                if(!d[E.v] && E.c>E.flow){
                    q.push(E.v);
                    d[E.v]=d[u]+1;
                }
            }
        }
        if(!d[t]) return false;
        return true ;
    }
    int dfs(int s,int a){           //a  目前最小残量
        if(s==n*m || a==0) return a;
        int flow=0,f;
        for(int& i=cur[s];i<vec[s].size();i++){
            Edge &E = edges[vec[s][i]];
            if(d[E.v]!=d[s]+1) continue ;
            f=dfs(E.v,min(E.c-E.flow,a));
            if(f>0){
                edges[vec[s][i]].c-=f;
                edges[vec[s][i]^1].c+=f;
                a-=f;
                flow+=f;
                if(a==0) break ;
            }
        }
        return flow;
    }
    int Dinic(){
        int ans = 0;
        while(bfs(1,n*m)){
            memset(cur,0,sizeof(cur));
            ans+=dfs(1,INF);
        }
        return ans ;
    }
  • 相关阅读:
    Java并发编程有多难?这几个核心技术你掌握了吗?
    「mysql优化专题」主从复制面试宝典!面试官都没你懂得多!(11)
    「mysql优化专题」高可用性、负载均衡的mysql集群解决方案(12)
    「mysql优化专题」什么是慢查询?如何通过慢查询日志优化?(10)
    「mysql优化专题」详解引擎(InnoDB,MyISAM)的内存优化攻略?(9)
    vsftp虚拟用户配置
    Oracle shrink space
    linux加程序是否当掉检测脚本
    oracke创建db link
    Oracle函数日期转换成秒(时间戳)
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/10047872.html
Copyright © 2011-2022 走看看