zoukankan      html  css  js  c++  java
  • 【网络流24题】飞行员配对方案问题

    题目描述

    英国皇家空军从沦陷国征募了大量外籍飞行员。由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1名是外籍飞行员。在众多的飞行员中,每一名外籍飞行员都可以与其他若干名英国飞行员很好地配合。如何选择配对飞行的飞行员才能使一次派出最多的飞机。对于给定的外籍飞行员与英国飞行员的配合情况,试设计一个算法找出最佳飞行员配对方案,使皇家空军一次能派出最多的飞机。

    对于给定的外籍飞行员与英国飞行员的配合情况,编程找出一个最佳飞行员配对方案,使皇家空军一次能派出最多的飞机。


    输入

    第 1 行有 2 个正整数 m 和 n。n 是皇家空军的飞行员总数(n<100);m 是外籍飞行员数(m<=n)。外籍飞行员编号为 1~m;英国飞行员编号为 m+1~n。

    接下来每行有 2 个正整数 i 和 j,表示外籍飞行员 i 可以和英国飞行员 j 配合。最后以 2个-1 结束。


    输出

    第 1 行是最佳飞行员配对方案一次能派出的最多的飞机数 M。接下来 M 行是最佳飞行员配对方案。每行有 2个正整数 i 和 j,表示在最佳飞行员配对方案中,飞行员 i 和飞行员 j 配对。如果所求的最佳飞行员配对方案不存在,则输出‘No Solution!’。


    样例输入

    5 10
    1 7
    1 8
    2 6
    2 9
    2 10
    3 7
    3 8
    4 7
    4 8
    5 10
    -1 -1


    样例输出

    4
    1 7
    2 9
    3 8
    5 10

    本题有spj



    题解

    最大流。

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    
    const int maxn=200+50;
    const int maxm=40000*2+50;
    const int inf=2e9;
    
    int n,m,s=0,t=201,d[maxn],x,a,b,maxflow;
    int fir[maxn],nex[maxm],to[maxm],wi[maxm],ecnt;
    
    queue<int> q;
    
    void add(int u,int v,int w){
        nex[ecnt]=fir[u];fir[u]=ecnt;to[ecnt]=v;wi[ecnt++]=w;
    }
    
    int bfs(){
        memset(d,0,sizeof(d));
        d[s]=1;
        q.push(s);
        while(!q.empty()){
            int u=q.front();q.pop();
            for(int e=fir[u];~e;e=nex[e]){
                int v=to[e];
                if(wi[e]&&!d[v]){
                    d[v]=d[u]+1;
                    q.push(v);
                }
            }
        }
        return d[t];
    }
    
    int dfs(int u,int flow){
        if(u==t) return flow;
        if(d[u]>=d[t]) return 0;
        for(int e=fir[u];~e;e=nex[e]){
            int v=to[e];
            if(wi[e]&&d[v]==d[u]+1&&(x=dfs(v,min(flow,wi[e])))){
                wi[e]-=x;
                wi[e^1]+=x;
                return x;
            }
        }
        return 0;
    }
    
    template<typename T>inline void read(T &aa){
        ll ff=1;char cc=getchar();aa=0;
        while((cc<'0'||cc>'9')&&cc!='-') cc=getchar();
        if(cc=='-') ff=-1,cc=getchar();
        while(cc<='9'&&cc>='0') aa=aa*10+cc-48,cc=getchar();
        aa*=ff;
    }
    
    int main(){
        read(m),read(n);
        memset(fir,-1,sizeof(fir));
        while(scanf("%d%d",&a,&b)==2&&(a!=-1||b!=-1)){
            add(a,b,1);add(b,a,0);
        }
        for(int i=1;i<=m;i++) add(s,i,1),add(i,s,0);
        for(int i=m+1;i<=n;i++) add(i,t,1),add(t,i,0);
        while(bfs()) while(dfs(s,inf)) maxflow+=x;
        if(maxflow==0){
            cout<<"No Solution!"<<endl;
            return 0;
        }
        cout<<maxflow<<endl;
        for(int i=1;i<=m;i++)
        for(int e=fir[i];~e;e=nex[e]){
            int v=to[e];
            if(v&&v<=n&&!wi[e]) cout<<i<<" "<<v<<endl;
        }
        return 0;
    }
  • 相关阅读:
    c语言中程序的循环控制 大小值的判断及赋值
    python中猜数字小游戏
    R语言中自编函数(例题)
    c语言中continue语句
    c语言中程序的循环控制 变量的非常规变化例题
    python中向列表中添加元素
    mean
    python中原始字符串和长字符串
    ArcInfo 的工作空间和 Coverage
    ArcGIS资料大全
  • 原文地址:https://www.cnblogs.com/rlddd/p/9925581.html
Copyright © 2011-2022 走看看