[luoguP2526] [SHOI2001]小狗散步(二分图最大匹配)

Posted 蒟蒻zht的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[luoguP2526] [SHOI2001]小狗散步(二分图最大匹配)相关的知识,希望对你有一定的参考价值。

传送门

 

简直就是模板题啊!

 

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 101

using namespace std;

int n, m, cnt;
int X1[N], Y1[N], X2[N], Y2[N], head[N], to[N * N], nex[N * N], belong[N], ans[N];
bool vis[N];

inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1;
	for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘;
	return x * f;
}

inline void add(int x, int y)
{
	to[cnt] = y;
	nex[cnt] = head[x];
	head[x] = cnt++;
}

inline double dis(double a, double b, double c, double d)
{
	return sqrt((a - c) * (a - c) + (b - d) * (b - d));
}

inline bool dfs(int u)
{
	int i, v;
	for(i = head[u]; ~i; i = nex[i])
	{
		v = to[i];
		if(!vis[v])
		{
			vis[v] = 1;
			if(!belong[v] || dfs(belong[v]))
			{
				belong[v] = u;
				return 1;
			}
		}
	}
	return 0;
}

inline int solve()
{
	int i, ret = 0;
	for(i = 1; i < n; i++)
	{
		memset(vis, 0, sizeof(vis));
		ret += dfs(i);
	}
	return ret;
}

int main()
{
	int i, j;
	double x, y, z;
	n = read();
	m = read();
	memset(head, -1, sizeof(head));
	for(i = 1; i <= n; i++)
		X1[i] = read(), Y1[i] = read();
	for(i = 1; i <= m; i++)
		X2[i] = read(), Y2[i] = read();
	for(i = 1; i < n; i++)
	{
		x = dis(X1[i], Y1[i], X1[i + 1], Y1[i + 1]);
		for(j = 1; j <= m; j++)
		{
			y = dis(X1[i], Y1[i], X2[j], Y2[j]);
			z = dis(X1[i + 1], Y1[i + 1], X2[j], Y2[j]);
			if(x * 2 + 1e-9 >= y + z) add(i, j);
		}
	}
	printf("%d\n", solve() + n);
	for(i = 1; i <= m; i++) ans[belong[i]] = i;
	for(i = 1; i <= n; i++)
	{
		printf("%d %d ", X1[i], Y1[i]);
		if(ans[i]) printf("%d %d ", X2[ans[i]], Y2[ans[i]]);
	}
	return 0;
}

  

以上是关于[luoguP2526] [SHOI2001]小狗散步(二分图最大匹配)的主要内容,如果未能解决你的问题,请参考以下文章

[P2526][SHOI2001]小狗散步

LuoguP4279 [SHOI2008]小约翰的游戏

LuoguP2161 [SHOI2009]会场预约

[LuoguP2161[ [SHOI2009]会场预约 (splay)

bzoj4596/luoguP4336 [SHOI2016]黑暗前的幻想乡(矩阵树定理,容斥)

[题解] LuoguP4284 [SHOI2014]概率充电器