zoukankan      html  css  js  c++  java
  • [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
    ", 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;
    }
    

      

  • 相关阅读:
    hdu1561 The more, The Better 树形DP+分组背包
    hdu1520 Anniversary party 简单树形DP
    hdu1054 Strategic Game 树形DP
    hdu1011 Starship Troopers 树形DP
    hdu4681 String DP(2013多校第8场)
    zoj3469 Food Delivery 区间DP
    LightOJ 1422 -Halloween Costumes 区间DP
    hdu4283 You Are the One 区间DP
    poj1651 Multiplication Puzzle 区间DP
    codeforce 149D Coloring Brackets 区间DP
  • 原文地址:https://www.cnblogs.com/zhenghaotian/p/8260917.html
Copyright © 2011-2022 走看看