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名是外 ...
随机推荐
- Scrapy模拟登录信息
携带cookie模拟登录 需要在爬虫里面自定义一个start_requests()的函数 里面的内容: def start_requests(self): cookies = '真实有效的cookie ...
- php token验证范例
<?php $module = $_GET['module']; $action = $_GET['action']; $token = md5sum($module.date('Y-m-d', ...
- mysql面试(1)
一一个 SQL 执行行行的很慢,我们要分两种情况讨论:1.大大多数情况下很正常,偶尔很慢,则有如下原因(1).数据库在刷新脏⻚页,例例如 redo log 写满了了需要同步到磁盘.(2).执行行行的时 ...
- Eclipse Mac OS 安装 Subversion插件subclipse 缺失JavaHL解决方案
安装 SVN 插件 subclipse 时可能遇到问题 subclipse 安装完成后,当我们选择使用 的时候还是会提示:javaHL not available, SVN接口选择 client:选择 ...
- HyperLeger Fabric开发(三)——HyperLeger Fabric架构
HyperLeger Fabric开发(三)--HyperLeger Fabric架构 一.HyperLeger Fabric逻辑架构 1.HyperLeger Fabric逻辑架构简介 Fabric ...
- HDU - 1253 胜利大逃亡(搜索)
Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在( ...
- RF(IF 判断)
1.关键字 Run Keyword If,格式如下: ELSE 必须大写 ELSE 前面需要加 "..." 表示缩进 Run Keyword If a == b log T ...
- Java中常用的获取从当前月开始的前第i个月、取结束时间与开始时间相差多少个月份等的方法
@RunWith(SpringRunner.class) @SpringBootTest public class DateTest { @Test public void test(){ DateF ...
- Kubernetes实战总结
>>> 目录 <<< 一.概述二.核心组件三.基本概念四.系统架构五.镜像制作六.服务编排七.持续部署八.故障排查 >>> 正文 << ...
- B. Preparing for Merge Sort
\(考虑的时候,千万不能按照题目意思一组一组去模拟\) \(要发现每组的最后一个数一定大于下一组的最后一个数\) \(那我们可以把a中的数一个一个填充到vec中\) #include <bits ...