输入输出样例
输入
4 5
1 4 5 7 5 2 −2 4
−4 −2 3 9 1 2 −1 3 8 −3
预期输出
6
1 4 3 9 5 7 5 2 1 2 −2 4
实际输出
6
1 4 1 2 5 7 5 2 3 9 −2 4
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int m,ans[110],match[110];
bool vis[110],e[110][110];
struct node
{
double x,y;
}grant[110],doge[110];
bool dfs(int u)
{
for(int i=1;i<=m;i++)
{
if(e[u][i]&&!vis[i])
{
vis[i]=true;
if(!match[i]||dfs(match[i]))
{
match[i]=u;
ans[u]=i;
return true;
}
}
}
return false;
}
inline double get_dist(const node &a,const node &b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
int n,cnt=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%lf%lf",&grant[i].x,&grant[i].y);
}
for(int i=1;i<=m;i++)
{
scanf("%lf%lf",&doge[i].x,&doge[i].y);
for(int j=1;j<n;j++)
{
if(2.0*get_dist(grant[j],grant[j+1])>=get_dist(grant[j],doge[i])+get_dist(doge[i],grant[j+1]))
{
e[j][i]=true;
}
}
}
for(int i=1;i<n;i++)
{
for(int j=1;j<=m;j++)
{
vis[j]=false;
}
if(dfs(i)&&++cnt==m)
{
break ;
}
}
printf("%d\n",n+cnt);
for(int i=1;i<=n;i++)
{
printf("%.0lf %.0lf ",grant[i].x,grant[i].y);
if(ans[i])
{
printf("%.0lf %.0lf ",doge[ans[i]].x,doge[ans[i]].y);
}
}
return 0;
}