[FZYZOJ 1354] 8-1 飞行员配对方案问题

P1354 -- 8-1 飞行员配对方案问题

时间限制:1000MS

内存限制:131072KB

Description

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

Input Format

第1 行有2个正整数m和n。n是皇家空军的飞行员总数(n<100);m是外籍飞行员数。外籍飞行员编号为1~m;英国飞行员编号为m+1~n。接下来每行有2 个正整数i和j,表示外籍飞行员i可以和英国飞行员j配合。最后以2个-1 结束。

Output Format

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

Sample 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

Sample Output

4
1 7
2 9
3 8
5 10

Hint

 【题解】

二分图最大匹配问题,学习了下匈牙利算法,算是填了个坑。

 1 #include <stdio.h>
 2 #include <string.h>
 3 using namespace std;
 4 
 5 const int V=210,E=70010;
 6 int head[V], next[E], to[E], tot;
 7 int fa[V],n,m,ans;
 8 bool vis[V];
 9 
10 bool hungry(int u) {
11     for (int i=head[u];i;i=next[i]) {
12         if(!vis[to[i]]) {
13             vis[to[i]]=1;
14             if(!fa[to[i]]||hungry(fa[to[i]])) {
15                 fa[to[i]]=u;
16                 return 1;
17             }
18         }
19     }
20     return 0;
21 }
22 
23 inline int g() {
24     int x=0,f=1;char ch=getchar();
25     while(ch<'0'||ch>'9') {
26         if(ch=='-') f=-1; 
27         ch=getchar();
28     }
29     while(ch>='0'&&ch<='9') {
30         x=(x<<1)+(x<<3)+ch-'0';
31         ch=getchar();
32     }
33     return x*f;
34 }
35 int main() {
36     int a,b;
37     m=g(),n=g();
38     while(1) {
39         a=g(),b=g();
40         if(a==-1||b==-1) break;
41         tot++;
42         to[tot]=b;
43         next[tot]=head[a];
44         head[a]=tot;
45     }
46     for (int i=1;i<=m;++i) {
47         memset(vis,0,sizeof(vis));
48         if(hungry(i)) ans++;
49     }
50     if(!ans) puts("No Solution!
");
51     else {
52         printf("%d
",ans);
53         for (int i=m+1;i<=m+n;++i) if(fa[i]) printf("%d %d
",fa[i],i);
54     }
55 }
View Code
原文地址:https://www.cnblogs.com/TonyNeal/p/fzyzoj1354.html