题解:

二分图最大匹配

根据三角形不等式

直接上最大匹配即可

注意编圈取相反数

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int N=110;
const double INF=0xffffffffffff,eps=1e-6;
struct Node{double x,y;}Dot1[N],Dot2[N];
double Dist(Node a,Node b)
{
return -sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int n,NX,NY,link[N],visx[N],visy[N];
double Map[N][N],lx[N],ly[N],slack[N];
int FindPath(int u)
{
visx[u]=1;
for (int i=1;i<=NY;i++)
{
if (visy[i])continue;
double temp=lx[u]+ly[i]-Map[u][i];
if (fabs(temp)<=eps)
{
visy[i]=1;
if (link[i]==-1||FindPath(link[i]))
{
link[i]=u;
return 1;
}
}
else if (slack[i]>temp)slack[i]=temp;
}
return 0;
}
void KM()
{
memset(lx,0,sizeof(lx));
memset(ly,0,sizeof(ly));
memset(link,-1,sizeof(link));
for (int i=1;i<=NX;i++)
for (int j=1;j<=NY;j++)
if (Map[i][j]>lx[i])lx[i]=Map[i][j];
for (int i=1;i<=NX;i++)
{
for (int j=1;j<=NY;j++)slack[j]=INF;
while (1)
{
memset(visx,0,sizeof(visx));
memset(visy,0,sizeof(visy));
if (FindPath(i))break;
double d=INF;
for (int j=1;j<=NY;j++)
if (!visy[j]&&d>slack[j])d=slack[j];
for (int j=1;j<=NX;j++)
if (visx[j])lx[j]-=d;
for (int j=1;j<=NY;j++)
{
if (visy[j])ly[j]+=d;
else slack[j]-=d;
}
}
}
}
int main()
{
while (~scanf("%d",&n))
{
memset(Map,0,sizeof(Map));
for (int i=1;i<=n;i++)scanf("%lf%lf",&Dot1[i].x,&Dot1[i].y);
for (int i=1;i<=n;i++)scanf("%lf%lf",&Dot2[i].x,&Dot2[i].y);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)Map[i][j]=Dist(Dot1[i],Dot2[j]);
NX=NY=n;
KM();
for (int i=1;i<=N;i++)
for (int j=1;j<=N;j++)
if (link[j]==i)
{
printf("%d\n",j);
break;
}
}
return 0;
}

  

poj3656的更多相关文章

随机推荐

  1. request.getQueryString()代表的含义

    在jsp做分页的时候,有时候我们想获取get请求链接中的参数保留下来. 比如客户端发送 http://localhost/test.do?a=b&c=d&e=f 通过request.g ...

  2. springCloud--1

    电影微服务是服务消费者,用户微服务是服务提供者. Springcloud对eureka的支持很好,eureka本身是一个基于REST的服务, Eureka里面有一个注册表,Application Cl ...

  3. python单元测试框架——pytest

    官网:https://docs.pytest.org/en/latest/ pytest帮你写出更好的程序 1.An example of a simple test:(一个简单的例子),命名为tes ...

  4. Linux CentOS6环境下MySQL5.1升级至MySQL5.5版本过程

    转载地址:http://www.laozuo.org/6145.html 老左今天有在帮朋友的博客搬迁到另外一台VPS主机环境,其环境采用的是LLSMP架构的,原先的服务器采用的是LNMP网站环境,其 ...

  5. Centos下Nginx配置WEB访问日志并结合shell脚本定时切割

    在一个成熟的WEB系统里,没有日志管理是不可以的,有了日志,可以帮助你得到用户地域来源.跳转来源.使用终端.某个URL访问量等相关信息:通过错误日志,你可以得到系统某个服务或server的性能瓶颈等. ...

  6. mysql5.6创建索引导致锁表阻塞查询

    结论:添加索引时,若果有对该表的慢查询,会导致索引添加延时等待   添加索引语句:alter table tb_name add index idx_xx(col_name);   执行添加索引的SQ ...

  7. unsigned short A = 10; printf("~A = %u\n", ~A); char c=128; printf("c=%d\n",c); 输出多少?

    这是题目给出的答案:第一题,-A =0xfffffff5,int值 为-11,但输出的是uint.所以输出4294967285 第二题,c=0x10,输出的是int,最高位为1,是负数,所以它的值就是 ...

  8. 混合开发的大趋势之一React Native之页面跳转

    转载请注明出处:王亟亟的大牛之路 最近事情有点多,没有长时间地连贯学习,文章也停了一个多礼拜,愧疚,有时间还是继续学习,继续写! 还是先安利:https://github.com/ddwhan0123 ...

  9. Codeforces Round #365 (Div. 2) B - Mishka and trip

    http://codeforces.com/contest/703/problem/B 题意: 每个点都有一个值,每条边的权值为这两个点相乘.1~n成环.现在有k个城市,城市与其他所有点都相连,计算出 ...

  10. Linux 下的jdk安装

    Linux 下安装JDK1.8 https://www.cnblogs.com/xuliangxing/p/7066913.html http://www.oracle.com/technetwork ...