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

    题目链接:

    题意:给出一副图,然后每条边的权值!然后问s到t的最小割

    solve:直接建图然后跑最大流就行了

    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define re register
    const int N=1e6+10;
    const int inf=1e9;
    inline void read(int &a)
    {
        a=0;
        int d=1;
        char ch;
        while(ch=getchar(),ch>'9'||ch<'0')
            if(ch=='-')
                d=-1;
        a=ch^48;
        while(ch=getchar(),ch>='0'&&ch<='9')
            a=(a<<3)+(a<<1)+(ch^48);
        a*=d;
    }
    int cur[N],head[N],cnt=-1,maxflow,n,m,dep[N];
    struct note{int next,v,dis;}edge[6*N];
    queue <int> q;
    inline void add(int u,int v,int dis)
    {
        edge[++cnt].next=head[u];
        edge[cnt].v=v;
        edge[cnt].dis=dis;
        head[u]=cnt;
        edge[++cnt].next=head[v];
        edge[cnt].v=u;
        edge[cnt].dis=dis;
        head[v]=cnt;
    }
    inline bool bfs(int s,int t)
    {
        while(!q.empty()) q.pop();
        for(re int i=1;i<=n*m;i++) dep[i]=inf+1,cur[i]=head[i];
        q.push(s);
        dep[s]=0;
        while(!q.empty())
        {
            int now=q.front();q.pop();
            for(re int i=head[now];i!=-1;i=edge[i].next)
            {
                if(dep[edge[i].v]>inf&&edge[i].dis)
                {
                    dep[edge[i].v]=dep[now]+1;
                    q.push(edge[i].v);
                }
            }
        }
        if(dep[t]<inf) return true;
        return false;
    }
    inline int dfs(int now,int t,int limit)
    {
        if(!limit||now==t) return limit;
        int flow=0,f;
        for(re int &i=cur[now];i!=-1;i=edge[i].next)
        {
            cur[now]=i;
            if(dep[now]+1==dep[edge[i].v]&&(f=dfs(edge[i].v,t,min(limit,edge[i].dis))))
            {
                flow+=f;
                edge[i].dis-=f;
                edge[i^1].dis+=f;
                limit-=f;
                if(!limit) break;
            }
        }
        return flow;
    }
    inline void dinic(int s,int t) {while(bfs(s,t)) maxflow+=dfs(s,t,inf);}
    int main()
    {
        read(n),read(m);
        for(re int i=1;i<=n*m;i++) head[i]=-1;
        for(re int i=1;i<=n;i++)
        {
            for(re int j=1;j<m;j++)
            {
                int x;read(x);
                add((i-1)*m+j,(i-1)*m+j+1,x);
            }
        }
        for(re int i=1;i<n;i++)
        {
            for(re int j=1;j<=m;j++)
            {
                int x;read(x);
                add((i-1)*m+j,i*m+j,x);
            }
        }
        for(re int i=1;i<n;i++)
        {
            for(re int j=1;j<m;j++)
            {
                int x;read(x);
                add((i-1)*m+j,i*m+j+1,x);
            }
        }
        dinic(1,n*m);
        printf("%d
    ",maxflow);
        return 0;
    }
  • 相关阅读:
    【CodeForces】[667]Pouring Rain
    【CodeForces】[669A]Little Artem and Presents
    【CodeForces】[675A]Infinite Sequence
    【CodeForces】[673A]Bear and Game
    【CodeForces】[651B]Beautiful Paintings
    【CodeForces】[621B]Wet Shark and Bishops
    【CodeForces】[673B]Problems for Round
    linux下怎么查看ssh的用户登录日志
    linux /etc/hosts文件作用
    使用ipmi进行服务器管理
  • 原文地址:https://www.cnblogs.com/acm1ruoji/p/12011048.html
Copyright © 2011-2022 走看看