解题报告 P2756 飞行员配对方案问题

· · 个人记录

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

解法:最大流

先随便给这些飞行员编个号。

比方说我令皇家空军编号为 1\sim n ,外籍飞行员编号为 n+1\sim n+m 。

怎么求解呢?

令源点 s 编号为 n+m+1 ,汇点 t 编号为 n+m+2 。

然后 s 向 t 跑最大流就好啦。

怎么输出呢?

遍历 s 的每一条正向边,满流的边所指向点就是被选上的点,再遍历这个点的所有出边,满流的边所指向的点就是与这个点匹配的那个点。

注意事项

AC 代码

评测记录。

#include<cstdio>
#include<cstring>
#include<bitset>
#include<queue>
#include<list>
namespace Qza{
    #define MAXN 205
    #define MAXM 200010
    #define INF 2147483647
    int h[MAXN],f[MAXN],gap[MAXN];
    int en[MAXN],to[MAXM],nex[MAXM],w[MAXM];
    std::list<int> lst[MAXN];
    std::vector<std::list<int>::iterator> it;
    void add_edge(int x,int y,int v)
    {
        static int cnt=-1;
        nex[++cnt]=en[x]; to[cnt]=y; en[x]=cnt; w[cnt]=v;
        nex[++cnt]=en[y]; to[cnt]=x; en[y]=cnt; w[cnt]=0;
    }
    int bfs(int s,int t)
    {
        register int u,v,hst=0;
        std::queue<int> q;
        for(q.push(t),h[t]=0;!q.empty();) {
            u=q.front(); q.pop();
            for(int p=en[u];p!=-1;p=nex[p]) 
                if(w[p^1] && !h[v=to[p]] && v!=t)
                    hst=h[v]=h[u]+1,q.push(v);
        }
        return hst;
    }
    int HLPP(int s,int t,int n)
    {
        if(s==t) return INF;
        register int u,v,flow,minhv,nowh=0,hst=bfs(s,t);
        if(!h[s]) return 0;
        it.resize(n+2);
        std::vector<int> buc[MAXN<<1];
        h[s]=n;
        for(int i=1;i<=n;++i)
            if(h[i]) ++gap[h[i]], it[i]=lst[h[i]].insert(lst[h[i]].begin(),i);
            else if(i!=t) h[i]=n<<1;
        for(int p=en[s];p!=-1;p=nex[p]) 
            if((v=to[p],flow=w[p])) {
                f[s]-=flow; f[v]+=flow;
                w[p]-=flow; w[p^1]+=flow;
                if(f[v]==flow) {
                    buc[h[v]].push_back(v);
                    nowh=std::max(nowh,h[v]);
                }
            }
        while(nowh) {
            if(buc[nowh].empty()) {--nowh;continue;}
            u=buc[nowh].back();
            buc[nowh].pop_back();
            minhv=n<<1;
            for(int p=en[u];f[u]&&p!=-1;p=nex[p]) {
                if(w[p]) {
                    if(h[v=to[p]]<h[u]) {
                        flow=std::min(f[u],w[p]);
                        f[u]-=flow; f[v]+=flow;
                        w[p]-=flow; w[p^1]+=flow;
                        if(f[v]==flow) buc[h[v]].push_back(v);
                    }
                    else minhv=std::min(minhv,h[v]);
                }
            }
            if(!f[u]) continue;
            if(h[u]<n && !(--gap[h[u]])) {
                for(int i=h[u]+1;i<=hst;++i) {
                    for(std::list<int>::iterator p=lst[i].begin();p!=lst[i].end();++p)
                        h[*p]=n+1;
                    gap[i]=0;
                    lst[i].clear();
                }
                hst=h[u]-1;
                lst[h[u]].clear();
                nowh=h[u]=n+1;
                buc[nowh].push_back(u);
            }
            else {
                if(h[u]<n) it[u]=lst[h[u]].erase(it[u]);
                nowh=h[u]=minhv+1;
                if(h[u]<n) {
                    it[u]=lst[nowh].insert(lst[nowh].begin(),u);
                    ++gap[nowh];
                    hst=std::max(hst,h[u]); 
                }
                buc[nowh].push_back(u);
            }
        }
        return f[t];
    }
    int dfs(int s,int x)
    {
        for(int p=en[x];p!=-1;p=nex[p])
            if(!w[p] && to[p]!=s) return to[p];
    }
    int read()
    {
        char ch=getchar();int x=0,f=1;
        while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') x=(x*10)+(ch^48),ch=getchar();
        return x*f;
    }
    void main()
    {
        memset(en,0xff,sizeof(en));
        int n,m,s,t,u,v,maxflow;
        m=read(); n=read();
        s=n+m+1; t=n+m+2;
        for(int i=1;i<=m;++i) add_edge(s,i,1);
        for(int i=1;i<=n;++i) add_edge(i+m,t,1);
        while(u=read(),v=read(),u!=-1) {
            add_edge(u,m+v,1);
        }
        maxflow=HLPP(s,t,n+m+2);
        if(!maxflow) {
            printf("No Solution!");
            return ;
        }
        printf("%d\n",maxflow);
        for(int p=en[s];p!=-1;p=nex[p])
            if(!w[p] && !f[to[p]]) printf("%d %d\n",to[p],dfs(s,to[p])-m);
        return ;
    }
}
int main()
{
    Qza::main();
    return 0;
}