P2756 飞行员配对方案问题 (最大流-二分图最大匹配)

题目来源

P2762 太空飞行计划问题

题目分析

中文题,题意就不用说了吧...

思路:经典二分图匹配问题,我们记M表示外籍飞行员的顶点集合,用N表示英国飞行员的顶点集合,E表示匹配关系,二分图G = (M\cupN,E),则对于任意的 m \inM,n\inN, 外籍飞行员m和英国飞行员n匹配\Leftrightarrow(m,n) \in E

而G中满足两两不含公共端点的变的集合K\subseteqE的基数|K|的最大值 ,就是我们需要求的最大匹配数(题目求的是最大匹配数的两倍)

我们可以将二分图最大匹配问题看成是一种特殊的最大流问题,建图过程如下:

将原图中所有的无向边e改成有向边,方向为从M到N,容量为1。增加源点s和汇点t,从s向M中每个点连一条容量为1的边,从N中每个点向t连一条容量为1的边。

这样一来,我们构建的图G'中最大的s-t流的流量就是原来二分图G中的最大匹配的匹配数了。

下图为第一个样例的图,蓝线代表容量为1的边(未修改之前),红边组成增广路径。

求出了最大匹配,我们还需要知道哪些顶点是匹配的,借助上图,我们注意到如果最终在残余网络中有边 e (m,n) 的容量为0, m \inM,n\inN,那么 m,n为一个匹配,所以我们枚举M,N之间每一条边来得出答案。

代码区

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<string>
#include<fstream>
#include<vector>
#include<stack>
#include <map>
#include <iomanip>
#define bug cout << "**********" << endl
#define show(x,y) cout<<"["<<x<<","<<y<<"] "
//#define LOCAL = 1;
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll mod = 1e6 + 3;
const int Max = 1e6 + 10;

struct Edge
{
	int to, next, flow;
}edge[Max << 1];;

int n, m, a, b, s, t;
int head[Max], tot;
int dis[Max];
int ans;
bool vis[Max];

void init()
{
	memset(head, -1, sizeof(head));tot = 0;
	ans = 0;
}

void add(int u, int v, int flow)
{
	edge[tot].to = v;
	edge[tot].flow = flow;
	edge[tot].next = head[u];
	head[u] = tot++;
}

bool bfs()
{
	memset(dis, -1, sizeof(dis));
	dis[s] = 0;
	queue<int>q;
	q.push(s);
	while (!q.empty())
	{
		int u = q.front();q.pop();
		for (int i = head[u]; i != -1;i = edge[i].next)
		{
			int v = edge[i].to;
			if (dis[v] == -1 && edge[i].flow > 0)
			{
				dis[v] = dis[u] + 1;
				if (v == t) return true;
				q.push(v);
			}
		}
	}
	return false;
}

int dfs(int u, int flow_in)
{
	if (u == t) return flow_in;
	int flow_out = 0;
	for (int i = head[u]; i != -1;i = edge[i].next)
	{
		int v = edge[i].to;
		if (dis[v] == dis[u] + 1 && edge[i].flow > 0)
		{
			int flow_part = dfs(v, min(flow_in, edge[i].flow));
			if (flow_part == 0) continue;
			flow_in -= flow_part;
			flow_out += flow_part;
			edge[i].flow -= flow_part;
			edge[i ^ 1].flow += flow_part;
			if (flow_in == 0)break;
		}
	}
	return flow_out;
}

int max_val()
{
	int sum = 0;
	while (bfs())
	{
		sum += dfs(s, inf);
	}
	return sum;
}

int main()
{
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	while (scanf("%d%d", &m, &n) != EOF)
	{
		init();
		s = 0, t = n + 1;
		for (int i = 1;i <= m;i++)
		{
			add(s, i, 1);add(i, s, 0);	//由源点向外籍飞行员建边
		}
		for (int i = m + 1; i <= n;i++)
		{
			add(i, t, 1);add(t, i, 0);
		}
		while (scanf("%d%d", &a, &b) != EOF && a != -1 && b != -1)
		{
			add(a, b, 1);add(b, a, 0);
		}
		printf("%d\n", max_val());
		for (int u = 1;u <= m;u++)
		{
			for (int i = head[u]; i != -1;i = edge[i].next)
			{
				if (edge[i].flow == 0 && edge[i].to != s && edge[i].to != t)
				{
					printf("%d %d\n", u, edge[i].to);
				}
			}
		}
	}
	return 0;
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值