飞行员配对问题[网络流24题之1]
来源:互联网 发布:音乐佳葫芦丝淘宝网 编辑:程序博客网 时间:2024/05/21 13:53
问题描述:
第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员。由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的
编程任务:
对于给定的外籍飞行员与英国飞行员的配合情况,编程找出一个最佳飞行员配对方案,使皇家空军一次能派出最多的飞机。
数据输入:
文件第
接下来每行有
结果输出 :
第
如果所求的最佳飞行员配对方案不存在,则输出
输入文件示例 :
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
分析 :
这个题目是一个经典的网络流问题,若把英国飞行员和外籍飞行员之间连一条边就是一个多源点多汇点的网络流问题,建模过程如下
1 :把配对的飞行员之间连一条容量为1 的边
2 :新增一个源S ,把S 与外籍飞行员之间连一条容量为1 的边
3 :新增一个汇T ,把T 与英国飞行员之间连一条容量为1 的边
最后求出图中的最大流即为最多配对数。求最大流我用的是
至于输出配对方案的话,直接在图中寻找那些流量为
代码 :
#include <cstdio>#include <cstring> #include <algorithm>#include <queue>using namespace std;const int inf = 0x3f3f3f3f;int n,m;int tot = 1;int ans;int c[1147];int head[1147],nxt[2247],to[2247],wei[2247];int cur[1147];queue<int> que;int read();void add(int,int);bool bfs();int dinic(int,int);int main(){ n=read();m=read(); for(int i=1;i<=n;++i) add(1,i+1); for(int i=n+1;i<=m;++i) add(i+1,m+2); int a,b; a=read();b=read(); while(a!=-1 && b!=-1){ a++;b++; add(a,b); a=read();b=read(); } int flag; while(bfs()){ flag = 1; while(flag){ memcpy(cur,head,sizeof head); flag = dinic(1,inf); ans += flag; } } if(ans){ printf("%d",ans); for(int i=head[1];i;i=nxt[i]) if(!wei[i]) for(int j=head[to[i]];j;j=nxt[j]) if(!wei[j]){ printf("\n%d %d",to[i],to[j]); break; } } else printf("No Solution!"); return 0;}int read(){ int in = 0; int flag = 1; char ch = getchar(); while(ch>'9'||ch<'0'){ if(ch=='-'){ ch = getchar(); flag = -1; break; } ch = getchar(); } while(ch>='0'&&ch<='9'){ in = in*10+ch-'0'; ch = getchar(); } return in*flag;}void add(int from,int tp){ ++tot;nxt[tot]=head[from];head[from]=tot;to[tot]=tp;wei[tot]=1; ++tot;nxt[tot]=head[tp];head[tp]=tot;to[tot]=from;wei[tot]=0;}bool bfs(){ int now; memset(c,0,sizeof c); que.push(1); c[1] = 1; do{ now = que.front(); for(int i=head[now];i;i=nxt[i]) if(!c[to[i]] && wei[i]){ c[to[i]] = c[now]+1; que.push(to[i]); } que.pop(); }while(!que.empty()); return c[m+2];}int dinic(int place,int low){ if(place == m+2) return low; int lower; for(int &i=cur[place];i;i=nxt[i]) if(c[to[i]]==c[place]+1 && wei[i]){ lower = dinic(to[i],min(low,wei[i])); if(lower){ wei[i] -= lower; wei[i^1] += lower; return lower; } } return 0;}
- 飞行员配对问题[网络流24题之1]
- 网络流24题之飞行员配对方案问题
- 网络流24题之飞行员配对方案问题
- 网络流24题之T1 飞行员配对方案问题
- 网络流24题之 飞行员配对方案问题 题解
- [网络流24题 #1]飞行员配对方案问题
- 网络流24题1 飞行员配对方案问题
- 网络流24题1 飞行员配对方案问题
- 网络流24题1 飞行员配对方案问题
- 网络流24题1 飞行员配对方案问题
- 网络流1飞行员配对方案问题
- kyeremal-网络流24题T1-飞行员配对方案问题
- 【网络流24题】飞行员配对方案问题
- 【网络流24题】飞行员配对方案问题
- [网络流24题]T1 飞行员配对方案问题
- 网络流24题——飞行员配对方案问题
- 线性规划与网络流24题 飞行员配对方案问题
- 【网络流24题】1.飞行员配对方案问题
- 顺时针或逆时针旋转180度等于先作一个水平镜像,再作一个垂直镜像!
- 华中农业大学第四届程序设计大赛网络同步赛
- linux C 多线程获取字符输入方法
- sqlserver查询前1000行
- Linux下开发C#程序
- 飞行员配对问题[网络流24题之1]
- 将本地的mongodb迁移到阿里云的mongodb上
- Extjs之自动消失提示框(Ext.example.msg)
- Spring Assert(方法入参检测工具类-断言)
- Java程序员新手老手都离不开八大开发工具
- c++多项式计算器
- List,Set,Map用法区别 以及 hashmap hashtable区别
- uploadifive 改进版 用于HTML5手机网站上传图片
- DMA传输中的内存对齐