2175. 飞行员配对方案问题

题目链接

2175. 飞行员配对方案问题

第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员。

由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的 2 名飞行员,其中 1 名是英国飞行员,另 1 名是外籍飞行员。

在众多的飞行员中,每一名外籍飞行员都可以与其他若干名英国飞行员很好地配合。

如何选择配对飞行的飞行员才能使一次派出最多的飞机。

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

输入格式

1 行有 2 个正整数 mnm 是外籍飞行员数;n皇家空军的飞行员总数。

外籍飞行员编号为 1m;英国飞行员编号为 m+1n

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

文件最后以 21 结束。

输出格式

1 行是最佳飞行员配对方案一次能派出的最多的飞机数 M

接下来 M 行是最佳飞行员配对方案。

每行有 2 个正整数 ij,表示在最佳飞行员配对方案中,外籍飞行员 i英国飞行员 j 配对。

如果有多种配对方案,则输出任意一种即可,方案内部配对输出顺序随意。

数据范围

1<m<n<100

输入样例:

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

解题思路

匈牙利算法

显然,本题是二分图求最大匹配模板题,用匈牙利算法(本质上是 EK 算法)求解即可

  • 时间复杂度:O(nm)

dinic

二分图求最大匹配,另外添加源点和汇点,即源点连接所有的左部点,所有的右部点连接汇点,且所有的边权值都为 1,不难发现:本题即求解最大流,直接用 dinic 算法求解即可

  • 时间复杂度:O(nm)

代码

  • 匈牙利算法
// Problem: 飞行员配对方案问题 // Contest: AcWing // URL: https://www.acwing.com/problem/content/description/2177/ // Memory Limit: 64 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=105,M=10005,inf=0x3f3f3f3f; int n,m,cur[N],d[N],s,t; int h[N],e[M],ne[M],f[M],idx; int q[N],hh,tt; void add(int a,int b,int c) { e[idx]=b,f[idx]=c,ne[idx]=h[a],h[a]=idx++; e[idx]=a,f[idx]=0,ne[idx]=h[b],h[b]=idx++; } int dfs(int x,int limit) { if(x==t)return limit; int flow=0; for(int i=cur[x];~i&&flow<limit;i=ne[i]) { cur[x]=i; int y=e[i]; if(d[y]==d[x]+1&&f[i]) { int t=dfs(y,min(f[i],limit-flow)); if(!t)d[y]=-1; f[i]-=t,f[i^1]+=t,flow+=t; } } return flow; } bool bfs() { memset(d,-1,sizeof d); d[s]=hh=tt=0; q[0]=s; cur[s]=h[s]; while(hh<=tt) { int x=q[hh++]; for(int i=h[x];~i;i=ne[i]) { int y=e[i]; if(d[y]==-1&&f[i]) { d[y]=d[x]+1; cur[y]=h[y]; if(y==t)return true; q[++tt]=y; } } } return false; } int dinic() { int res=0,flow=0; while(bfs())while(flow=dfs(s,inf))res+=flow; return res; } int main() { scanf("%d%d",&m,&n); memset(h,-1,sizeof h); s=0,t=n+1; for(int i=1;i<=m;i++)add(s,i,1); for(int i=m+1;i<=n;i++)add(i,t,1); int a,b; while(scanf("%d%d",&a,&b),a!=-1)add(a,b,1); printf("%d\n",dinic()); for(int i=0;i<idx;i+=2) if(e[i]>=m+1&&e[i]<=n&&!f[i])printf("%d %d\n",e[i^1],e[i]); return 0; }
  • dinic
// Problem: 飞行员配对方案问题 // Contest: AcWing // URL: https://www.acwing.com/problem/content/description/2177/ // Memory Limit: 64 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=105; int match[N*2],n,m,res; vector<int> adj[N]; bool st[N*2]; bool find(int x) { for(int y:adj[x]) { if(!st[y]) { st[y]=true; if(match[y]==0||find(match[y])) { match[y]=x; return true; } } } return false; } int main() { scanf("%d%d",&m,&n); int a,b; while(scanf("%d%d",&a,&b),a!=-1&&b!=-1) adj[a].pb(b); for(int i=1;i<=m;i++) { memset(st,0,sizeof st); if(find(i))res++; } printf("%d\n",res); for(int i=m+1;i<=n;i++) if(match[i])printf("%d %d\n",match[i],i); return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16550146.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(57)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示