• [网络流24题] 飞行员配对方案问题


    ~~~题面~~~

    题解:

    开始复习以前写的题emmm

    裸的二分图最大匹配,网络流也可以跑。

    没有什么好解释的。。。

    下面是匈牙利的做法,如果用网络流就加一个超级源汇

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 struct abc{
     4     int date,next;
     5 };
     6 struct ab{
     7     int us,other;
     8 };
     9 abc chain[11000];
    10 int tot,n,m,totp,Head[110],link[110],ans;
    11 ab pr[110];
    12 bool z[110];
    13 int read()
    14 {
    15     bool flag=false;int x=0;char c=getchar();
    16     while((c>'9'||c<'0') && c!='-')c=getchar();
    17     if(c=='-')flag=true,c=getchar();
    18     while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
    19     if(!flag) return x;
    20     else return -x;
    21 }
    22 void add(int f,int w)
    23 {
    24     chain[++tot].date=w;
    25     chain[tot].next=Head[f];
    26     Head[f]=tot;
    27 }
    28 bool cmp(ab a,ab b)
    29 {
    30     return a.other<b.other;
    31 }
    32 void pre()
    33 {
    34     int a,b;
    35     n=read(),m=read();//n为外籍,m为总数
    36     while(n)
    37     {
    38         a=read(),b=read();
    39         if(a==-1 && b==-1)break;
    40         add(a,b);
    41     }
    42 }
    43 bool DFS(int x)
    44 {
    45     int now;
    46     for(int i=Head[x]; i ;i=chain[i].next)
    47     {
    48         now=chain[i].date;
    49         if(!z[now])
    50         {
    51             z[now]=true;
    52             if(!link[now] || DFS(link[now]))//link[本国]=外籍的连线
    53             {
    54                 link[now]=x;
    55                 return true;
    56             }
    57         }
    58     }
    59     return false;
    60 }
    61 void work()
    62 {
    63     for(int i=1; i<=n ;i++)
    64     {
    65         memset(z,0,sizeof(z));
    66         if(DFS(i))ans++;
    67     }
    68     printf("%d
    ",ans);
    69     for(int i=n+1; i<=m ;i++)
    70     {
    71         if(link[i])pr[++totp].us=i,pr[totp].other=link[i];
    72     }
    73     sort(pr+1,pr+totp+1,cmp);
    74     for(int i=1; i<=totp ;i++)
    75     {
    76         printf("%d %d
    ",pr[i].other,pr[i].us);
    77     }
    78 }
    79 int main()
    80 {
    81     pre();
    82     work();
    83     return 0;
    84 }
  • 相关阅读:
    Windows 7 语音识别和屏幕录像
    用户体验研究乱谈后台导航
    安装VS2010
    lucene.net搜索索引详解
    网站秒杀那点破事
    初次给鼠标手术
    网站页面回车和鼠标焦点
    利用C语言的部分初始化特性进行字符串的全部初始化。
    QoS的各个方面
    指针和数组错用的问题汇总
  • 原文地址:https://www.cnblogs.com/ww3113306/p/9366965.html
Copyright © 2020-2023  润新知