zoukankan      html  css  js  c++  java
  • 51nod2006 飞行员配对(二分图最大匹配)

    题目来源: 网络流24题
    基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
     收藏
     关注

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



    Input
    第1行有2个正整数 m 和 n。n 是皇家空军的飞行 员总数(n<100);m 是外籍飞行员数。外籍飞行员编号为 1~m;英国飞行员编号为 m+1~n。接下来每行有 2 个正整数 i 和 j,表示外籍飞行员 i 可以和英国飞行员 j 配合。输入最后以 2 个-1 结束。
    Output
    第 1 行是最佳飞行 员配对方案一次能派出的最多的飞机数 M。如果所求的最佳飞行员配对方案不存在,则输出‘No Solution!’。
    Input示例
    5 10
    1 7
    1 8
    2 6
    2 9
    2 10
    3 7
    3 8
    4 7
    4 8
    5 10
    -1 -1
    Output示例
    4

    题意:中文题!

    思路:直接二分图模板题

    #include <iostream>
    #include<vector>
    #include<algorithm>
    #include<string.h>
    using namespace std;
    int V;//顶点数
    const int maxv=1000;
    vector<int>G[maxv];//图的邻接表
    int match[maxv];//所匹配的顶点
    bool used[maxv];//dfs中用到的标志
    
    void add_edge(int u,int v)
    {
        G[u].push_back(v);
        G[v].push_back(u);
    }
    //通过dfs寻找增广路
    bool dfs(int v)
    {
        used[v]=true;
        for(int i=0;i<G[v].size();i++)
        {
            int u=G[v][i],w=match[u];
            if(w<0||!used[w]&&dfs(w))
            {
                match[v]=u;
                match[u]=v;
                return true;
            }
        }
        return false;
    }
    //求二分图的最大匹配
    int bitpartite_matching()
    {
        int res=0;
        memset(match,-1,sizeof(match));
        for(int v=0;v<V;v++)
        {
            if(match[v]<0)
            {
                memset(used,0,sizeof(used));
                if(dfs(v))
                {
                    res++;
                }
            }
        }
        return res;
    }
    
    
    
    
    int main()
    {
        int n,m;
        cin>>n>>m;
        V=m+n;
        int n1,m1;
        while(cin>>n1>>m1&&n1!=-1&&m1!=-1)
        {
            add_edge(n1,m1);
        }
        int temp=bitpartite_matching();
        if(temp>0)
        cout<<temp<<endl;
        else cout<<"No Solution!"<<endl;
        return 0;
    }
    


  • 相关阅读:
    IP 协议
    以太网协议
    制作Win10系统安装U盘和安装纯净版Win10
    IP地址的配置
    进制转换
    设置QQ环境变量
    修改IE默认页的指向
    虚拟机安装Linux ubuntu19.10
    【Eclipse】Editor does not contain a main type
    Vmware Workstation虚拟机
  • 原文地址:https://www.cnblogs.com/bryce1010/p/9387371.html
Copyright © 2011-2022 走看看