P2756 飞行员配对方案问题 网络流
#include <bits/stdc++.h>
using namespace std;
const int maxn = , inf = 0x3f3f3f;
struct Edge {
int from, to, cap, flow;
}; struct Dinic {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn]; void AddEdge(int from, int to, int cap) {
edges.push_back((Edge){from, to, cap, });
edges.push_back((Edge){to, from, , });
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool bfs() {
memset(vis, , sizeof(vis));
queue<int> que;
que.push(s);
d[s] = ;
vis[s] = true;
while (!que.empty()) {
int x = que.front(); que.pop();
for (int i = ; i < G[x].size(); ++i) {
Edge& e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = true;
d[e.to] = d[x] + ;
que.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x, int a) {
if (x == t || a == ) return a;
int flow = , f;
for (int& i = cur[x]; i < G[x].size(); ++i) {
Edge& e = edges[G[x][i]];
if (d[x] + == d[e.to] && (f = dfs(e.to, min(a, e.cap-e.flow))) > ) {
e.flow += f;
edges[G[x][i]^].flow -= f;
flow += f;
a -= f;
if (a == ) break;
}
}
return flow;
}
int maxflow(int s, int t) {
this->s = s; this->t = t;
int flow = ;
while (bfs()) {
memset(cur,,sizeof(cur));
flow += dfs(s,inf);
}
return flow;
}
}dinic;
int main() {
int m, n; scanf("%d%d",&m,&n);
while (true) {
int i, j; scanf("%d%d",&i,&j);
if (i == - && j == -) break;
dinic.AddEdge(i,j,);
}
int s = n+, t = n+;
for (int i = ; i <= m; ++i) {
dinic.AddEdge(s,i,);
}
for (int i = m+; i <= n; ++i) {
dinic.AddEdge(i,t,);
}
int ans = dinic.maxflow(s,t);
if (ans == ) puts("No Solution!");
else {
printf("%d\n",ans);
for (int i = ; i < dinic.edges.size(); ++i) {
if (dinic.edges[i].from == s) continue;
if (dinic.edges[i].to == t) continue;
if (dinic.edges[i].flow == ) {
printf("%d %d\n",dinic.edges[i].from,dinic.edges[i].to);
}
}
}
return ;
}
P2756 飞行员配对方案问题 网络流的更多相关文章
- 洛谷 [P2756] 飞行员配对方案问题 网络流实现
网络流实现二分图匹配 对于x集合的每一个点连一条从源点出发的容量为一的边,对于y集合的每一个点连一条到汇点的容量为一的边,跑最大流 #include <iostream> #include ...
- 洛谷P2756 飞行员配对方案问题 网络流_二分图
Code: #include<cstdio> #include<queue> #include<vector> #include<cstring> #i ...
- 洛谷 P2756 飞行员配对方案问题 (二分图/网络流,最佳匹配方案)
P2756 飞行员配对方案问题 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其 ...
- 洛谷——P2756 飞行员配对方案问题
P2756 飞行员配对方案问题 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其 ...
- luogu P2756 飞行员配对方案问题
题目链接:P2756 飞行员配对方案问题 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另 ...
- 洛谷P2756飞行员配对方案问题 P2055假期的宿舍【二分图匹配】题解+代码
洛谷 P2756飞行员配对方案问题 P2055假期的宿舍[二分图匹配] 飞行员配对方案问题 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架 ...
- 洛谷P2756 飞行员配对方案问题(二分图匹配)
P2756 飞行员配对方案问题 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其 ...
- 洛谷 P2756 飞行员配对方案问题 (二分图匹配)
题目链接:P2756 飞行员配对方案问题 题意 给定 \(m\) 个外籍飞行员和 \(n - m\) 个英国飞行员,每一架飞机需要一名英国飞行员和一名外籍飞行员,求最多能派出几架飞机. 思路 最大流 ...
- P2756 飞行员配对方案问题(网络流24题之一)
题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1名是外 ...
随机推荐
- php---算法和数据结构
<?php header("content-type:text/html;charset=utf-8"); $arr = array(3,5,8,4,9,6,1,7,2); ...
- 在Spring Boot中配置web app
文章目录 添加依赖 配置端口 配置Context Path 配置错误页面 在程序中停止Spring Boot 配置日志级别 注册Servlet 切换嵌套服务器 在Spring Boot中配置web a ...
- 源码安装nginx 方法二
yum 仓库不能用大写字母 [root@oldboy conf.d]# gzip * 压缩当前目录下的所有文件 gzip ./* gzip . gzip./ # 关闭防火墙和selinux [root ...
- 腾讯视频怎么转成mp4模式 软件 工具 方法 最新【已解决】
1.搜索: 小白兔视频格式在线转换 2.转换好后视频已经是MP4格式了. 转载于:https://blog.51cto.com/14204019/2396896
- codeforce 227D Naughty Stone Piles (贪心+递归+递推)
Description There are n piles of stones of sizes a1, a2, -, an lying on the table in front of you. D ...
- App 自动化环境搭建
1.安装 Appium-desktop 工具 下载地址:https://github.com/appium/appium-desktop/releases 2.安装 Android 环境 安装 JDK ...
- IBM Rational Rose软件下载以及全破解方法
最近忙着作业,软件设计的类图着实难画,于是整理了rose的下载和破解方法 Rational Rose是Rational公司出品的一种面向对象的统一建模语言的可视化建模工具.用于可视化建模和公司级水平软 ...
- 线程池(Java中有哪些方法获取多线程)
线程池(Java中有哪些方法获取多线程) 前言 获取多线程的方法,我们都知道有三种,还有一种是实现Callable接口 实现Runnable接口 实现Callable接口 实例化Thread类 使用线 ...
- centos安装libconfig
安装很简单,生成的.so文件会被安装到/usr/local/lib目录,记得修改/etc/profile. 安装过程会出现两个错误: What is makeinfo, and how do I ge ...
- JS 究竟是先有鸡还是有蛋,Object与Function究竟谁出现的更早,Function算不算Function的实例等问题杂谈
壹 ❀ 引 我在JS 疫情宅在家,学习不能停,七千字长文助你彻底弄懂原型与原型链一文中介绍了JavaScript原型与原型链,以及衍生的__proto__.constructor等一系列属性.在解答了 ...