zoukankan      html  css  js  c++  java
  • 洛谷2756飞行员配对方案问题

    题目链接:飞行员配对方案问题

    很多人把这题当做二分图匹配的模板做,匈牙利的时间复杂度为(O(nm))

    但是如果用dinic去做时间就会是(O(sqrt nm))

    建立超级源点(s)(t),直接按照输入连边,源点和汇点分别连向一个点集即可

    #include<iostream>
    #include<string>
    #include<string.h>
    #include<stdio.h>
    #include<algorithm>
    #include<math.h>
    #include<vector>
    #include<queue>
    #include<map>
    using namespace std;
    #define maxd 1e9+7
    struct network_flows{
        struct node{
            int from,to,nxt,flow;
        }sq[100100];
        int all,dep[100100],head[100100],cur[100100],n,m,s,t;
        bool vis[100100];
    
       void init(int n,int m)
        {
            this->s=n+m+1;this->t=n+m+2;
            this->n=n+m+2;this->all=1;
            memset(head,0,sizeof(head));
        }
    
        void add(int u,int v,int w)
        {
            all++;sq[all].from=u;sq[all].to=v;sq[all].nxt=head[u];sq[all].flow=w;head[u]=all;
            all++;sq[all].from=v;sq[all].to=u;sq[all].nxt=head[v];sq[all].flow=0;head[v]=all;
        }
    
        bool bfs()
        {
            queue<int> q;int i;
            memset(vis,0,sizeof(vis));
            vis[s]=1;q.push(s);dep[s]=0;
            while (!q.empty())
            {
                int u=q.front();q.pop();
                for (i=head[u];i;i=sq[i].nxt)
                {
                    int v=sq[i].to;
                    if ((!vis[v]) && (sq[i].flow))
                    {
                        vis[v]=1;dep[v]=dep[u]+1;q.push(v);
                    }
                }
            }
            if (!vis[t]) return 0;
            for (i=1;i<=n;i++) cur[i]=head[i];
            return 1;
        }
    
        int dfs(int now,int to,int lim)
        {
            if ((!lim) || (now==to)) return lim;
            int i,sum=0;
            for (i=cur[now];i;i=sq[i].nxt)
            {
                cur[now]=i;int v=sq[i].to;
                if (dep[now]+1==dep[v])
                {
                    int f=dfs(v,to,min(lim,sq[i].flow));
                    if (f)
                    {
                        lim-=f;sum+=f;
                        sq[i].flow-=f;
                        sq[i^1].flow+=f;
                        if (!lim) break;
                    }
                }
            }
            return sum;
        }
    
        int work()
        {
            int ans=0;
            while (bfs()) ans+=dfs(s,t,maxd);
            return ans;
        }
    
        void out(int n,int m)
        {
            int i,j;
            for (i=1;i<=n;i++)
            {
                for (j=head[i];j;j=sq[j].nxt)
                {
                    if ((sq[j].to>n) && (sq[j].to<=n+m) && (!sq[j].flow))
                        printf("%d %d
    ",i,sq[j].to);
                }
            }
        }
    }dinic;
    int n,m,s,t;
    
    int read()
    {
        int x=0,f=1;char ch=getchar();
        while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
        while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
        return x*f;
    }
    
    void init()
    {
        n=read();m=read();int i,j,u,v;
        dinic.init(n,m);
        s=n+m+1;t=n+m+2;
        for (i=1;i<=n;i++) dinic.add(s,i,1);
        for (i=1;i<=m;i++) dinic.add(n+i,t,1);
        while ((scanf("%d%d",&u,&v)) && (u!=-1) && (t!=-1))
            dinic.add(u,v,1);
    }
    
    void work()
    {
        int ans=dinic.work();
        if (!ans) {printf("No Solution!");return;}
        printf("%d
    ",ans);
        dinic.out(n,m);
    }
    
    int main()
    {
        init();
        work();
        return 0;
    }
    
  • 相关阅读:
    A. Two Semiknights Meet DFS
    Dummy Guy 几何
    Electrification Plan MST
    Parallel and Perpendicular 几何,数学
    A. Soroban 暴力+水题
    B. Fence 前缀和 水题
    Max Sum http://acm.hdu.edu.cn/showproblem.php?pid=1003
    亲和串 http://acm.hdu.edu.cn/showproblem.php?pid=2203
    N! http://acm.hdu.edu.cn/showproblem.php?pid=1042
    Let the Balloon Rise http://acm.hdu.edu.cn/showproblem.php?pid=1004
  • 原文地址:https://www.cnblogs.com/encodetalker/p/10182563.html
Copyright © 2011-2022 走看看