码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-01-10 22:49:38      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:int   turn   print   void   body   printf   dfs   namespace   www.   

传送门

 

简直就是模板题啊!

 

#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]小狗散步(二分图最大匹配)

标签:int   turn   print   void   body   printf   dfs   namespace   www.   

原文地址:https://www.cnblogs.com/zhenghaotian/p/8260917.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!