zoukankan      html  css  js  c++  java
  • BZOJ 2502: 清理雪道 | 有上下界最小流

    #include<cstdio>
    #include<algorithm>
    #include<cstring> 
    #include<queue>
    #define N 405
    #define INF 0x3f3f3f3f
    #define M 200005
    using namespace std;
    int head[N],cur[N],n,m,S,T,d[N],s,t,sum,ecnt=1,lev[N],ans,dec;
    queue<int> q;
    struct adj
    {
        int nxt,v,w;
    }e[M];
    void add(int u,int v,int w)
    {
        e[++ecnt].v=v;e[ecnt].nxt=head[u];e[ecnt].w=w;head[u]=ecnt;
        e[++ecnt].v=u;e[ecnt].nxt=head[v];e[ecnt].w=0;head[v]=ecnt;
    }
    bool bfs()
    {
        for (int i=1;i<=T;i++)
            cur[i]=head[i],lev[i]=-1;
        q.push(S);lev[S]=1;
        while (!q.empty())
        {
            int u=q.front();q.pop();
            for (int i=head[u],v;i;i=e[i].nxt)
                if (lev[v=e[i].v]==-1 && e[i].w>0)
                    q.push(v),lev[v]=lev[u]+1;
        }
        return lev[T]!=-1;
    }
    int dfs(int u,int flow)
    {
        if (u==T) return flow;
        int ret=0,v,delta;
        for (int &i=cur[u];i;i=e[i].nxt)
            if (lev[v=e[i].v]==lev[u]+1 && e[i].w>0)
            {
                delta=dfs(v,min(flow-ret,e[i].w));
                if (delta)
                {
                    e[i].w-=delta;e[i^1].w+=delta;ret+=delta;
                    if (ret==flow) break;
                }
            }
        return ret;
    }
    int main()
    {
        scanf("%d",&n);
        s=n+1;t=n+2;S=t+1;T=S+1;add(t,s,INF);
        for (int i=1,u,v;i<=n;i++)
        {
            scanf("%d",&u);
            while (u--)
            {
                scanf("%d",&v);
                add(i,v,INF-1);
                d[i]--,d[v]++;
            }
        }
        for (int i=1;i<=n;i++)
            add(s,i,INF),add(i,t,INF);
        for (int i=1;i<=n;i++)
            if (d[i]>0) add(S,i,d[i]);
            else if (d[i]<0) add(i,T,-d[i]);
        while (bfs()) ans+=dfs(S,INF);
        for (int i=head[S];i;i=e[i].nxt) e[i].w=e[i^1].w=0;
        for (int i=head[T];i;i=e[i].nxt) e[i].w=e[i^1].w=0;
        sum=e[3].w;e[2].w=e[3].w=0;
        S=t;T=s;
        while (bfs()) dec+=dfs(S,INF);
        printf("%d
    ",sum-dec);
        return 0;
    }
  • 相关阅读:
    LeetCode 485. Max Consecutive Ones
    LeetCode 367. Valid Perfect Square
    LeetCode 375. Guess Number Higher or Lower II
    LeetCode 374. Guess Number Higher or Lower
    LeetCode Word Pattern II
    LeetCode Arranging Coins
    LeetCode 422. Valid Word Square
    Session 共享
    java NIO
    非阻塞IO
  • 原文地址:https://www.cnblogs.com/mrsheep/p/8215546.html
Copyright © 2011-2022 走看看