[网络流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\n",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\n",pr[i].other,pr[i].us);
77     }
78 }
79 int main()
80 {
81     pre();
82     work();
83     return 0;
84 }

 

posted @ 2018-07-25 17:01  ww3113306  阅读(126)  评论(0编辑  收藏  举报
知识共享许可协议
本作品采用知识共享署名-非商业性使用-禁止演绎 3.0 未本地化版本许可协议进行许可。