题目背景

第二次世界大战时期..

题目描述

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

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

输入输出格式

输入格式:

第 1 行有 2 个正整数 m 和 n。n 是皇家空军的飞行员总数(n<100);m 是外籍飞行员数。外籍飞行员编号为 1~m;英国飞行员编号为 m+1~n。

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

输出格式:

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

输入输出样例

输入样例#1:

5 10
1 7
1 8
2 6
2 9
2 10
3 7
3 8
4 7
4 8
5 10
-1 -1
输出样例#1:

4
1 7
2 9
3 8
5 10

思路:

  网络流24题之一;

  s到1~m连边,流量为1;

  t到m+1~m+n连边,流量为1;

  跑最大流;

  输出答案;

  然后根据边的流量输出配对方案;

来,上代码:

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> #define maxn 205
#define INF 0x7ffffff using namespace std; struct PrintType {
int first,second;
};
struct PrintType ans[maxn]; struct EdgeType {
int to,next,flow;
};
struct EdgeType edge[maxn*]; int if_z,head[maxn],s,t=maxn-,deep[maxn],cnt=,m,n; char Cget; inline void in(int &now)
{
now=,if_z=,Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
} inline void edge_add(int u,int v)
{
edge[++cnt].to=v,edge[cnt].flow=,edge[cnt].next=head[u],head[u]=cnt;
edge[++cnt].to=u,edge[cnt].flow=,edge[cnt].next=head[v],head[v]=cnt;
} bool BFS()
{
memset(deep,-,sizeof(deep));
queue<int>que;
que.push(s),deep[s]=;
while(!que.empty())
{
int pos=que.front();
for(int i=head[pos];i;i=edge[i].next)
{
if(deep[edge[i].to]<&&edge[i].flow>)
{
deep[edge[i].to]=deep[pos]+;
if(edge[i].to==t) return true;
que.push(edge[i].to);
}
}
que.pop();
}
return false;
} int flowing(int now,int flow)
{
if(now==t||flow<=) return flow;
int oldflow=;
for(int i=head[now];i;i=edge[i].next)
{
if(edge[i].flow<=||deep[edge[i].to]!=deep[now]+) continue;
int pos=flowing(edge[i].to,min(flow,edge[i].flow));
flow-=pos;
oldflow+=pos;
edge[i].flow-=pos;
edge[i^].flow+=pos;
if(flow==) return oldflow;
}
return oldflow;
} bool cmp(struct PrintType a,struct PrintType b)
{
return a.first<b.first;
} void dinic()
{
int pos=;
while(BFS()) pos+=flowing(s,INF);
if(pos==)
{
printf("No Solution!\n");
return ;
}
printf("%d\n",pos);
BFS();
cnt=;
for(int i=head[s];i;i=edge[i].next)
{
if(edge[i].flow==)
{
for(int j=head[edge[i].to];j;j=edge[j].next)
{
if(edge[j].flow==)
{
ans[++cnt].first=edge[i].to;
ans[cnt].second=edge[j].to;
}
}
}
}
sort(ans+,ans+cnt+,cmp);
for(int i=;i<=cnt;i++)
{
printf("%d %d\n",ans[i].first,ans[i].second);
}
} int main()
{
in(m),in(n);
int u,v;
in(u),in(v);
while(u!=-&&v!=-)
{
edge_add(u,v);
in(u),in(v);
}
for(int i=;i<=m;i++) edge_add(s,i);
for(int i=;i<=n;i++) edge_add(i+m,t);
dinic();
return ;
}

AC日记——飞行员配对方案问题 洛谷 P2756的更多相关文章

  1. AC日记——无线网络发射器选址 洛谷 P2038

    题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129 条东西向街道和129 条南北向街道所形成的网格状,并且相邻 ...

  2. AC日记——NASA的食物计划 洛谷 P1507

    题目背景 NASA(美国航空航天局)因为航天飞机的隔热瓦等其他安 全技术问题一直大伤脑筋,因此在各方压力下终止了航天 飞机的历史,但是此类事情会不会在以后发生,谁也无法 保证,在遇到这类航天问题时,解 ...

  3. AC日记——校门外的树 洛谷 P1047

    题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,……,L,都种 ...

  4. AC日记——小A的糖果 洛谷七月月赛

    小A的糖果 思路: for循环贪心: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #defi ...

  5. AC日记——矩阵取数游戏 洛谷 P1005

    矩阵取数游戏 思路: dp+高精: 代码: #include <bits/stdc++.h> using namespace std; #define ll long long struc ...

  6. AC日记——红色的幻想乡 洛谷 P3801

    红色的幻想乡 思路: 线段树+容斥原理: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #de ...

  7. AC日记——妖梦拼木棒 洛谷 P3799

    妖梦拼木棒 思路: 神特么题: 代码: #include <bits/stdc++.h> using namespace std; #define mod 1000000007LL int ...

  8. AC日记——妖梦斩木棒 洛谷 P3797

    妖梦斩木棒 思路: 略坑爹: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 #define m ...

  9. AC日记——[SDOI2009]HH去散步 洛谷 P2151

    [SDOI2009]HH去散步 思路: 矩阵快速幂递推(类似弗洛伊德): 给大佬跪烂-- 代码: #include <bits/stdc++.h> using namespace std; ...

随机推荐

  1. python入门:while 循环的基本用法

    #!/usr/bin/env python # -*- coding:utf-8 -*- #while 循环的作用 import time while True: ") time.sleep ...

  2. 【php】【异步】php实现异步的几种方法

    请参考  4种php常用的异步执行方式 ajax 和 img 的 src 属性 系统指令调用 (在php代码里面调用系统指令) curl socket通信 ​

  3. LeetCode(274)H-Index

    题目 Given an array of citations (each citation is a non-negative integer) of a researcher, write a fu ...

  4. java_时间戳与Date_相互转化

    [转自:http://blog.csdn.net/heng615975867/article/details/36016617] 1.时间戳的定义 时间戳是指文件属性里的创建.修改.访问时间. 数字时 ...

  5. 关于.net 项目 nuget包还原项目失败的记录

    在.net项目中,一般通过vs打开项目,会自动进行nuget包还原,可能因为其他一些因素,包还原失败,同时在之前包依赖是可以正常使用,现在却提示包 AutoMapper 6.2.2 与 netcore ...

  6. Scala学习-02-方法

    算数和操作符重载 所有的操作符都是方法. a + b 是一种缩写形式  :  a .+ b “+”是方法名(操作符重载) ++和—— Scala中并没有“++”和“——”.需要使用“+=”和“-=” ...

  7. python - 自动化测试框架 - logger

    # -*- coding:utf-8 -*- '''@project: Voctest@author: Jimmy@file: log.py@ide: PyCharm Community Editio ...

  8. python之路 --- python基础

    一.python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...

  9. 04-python进阶-map&reduce

    Map --映射 Reduce -- 归纳 将大数据标准化的处理 Map 拆封任务,Reduce将结果合并 这样是不是能够将很多计算机组成一台超级计算机呢? 一些问题:如果任务本身就很复杂,那么拆解任 ...

  10. Tomcat自动发布war包

    有两种方法: 1.将项目打成war包,复制到${tomcat.home}\webapps目录下.当tomcat启动时会自动将其解包. 有人说,不能直接将war文件夹直接复制到${tomcat.home ...