链接

给你两条线及两条线上的点,求最小生成树。

可以挨个枚举一条线上的点,三分出另一条线上离他最近的点进行连边。

注意N、M可能为0

debug了1天半,至今不知道原始二分版本错在哪里。。

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 50010
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
struct point
{
double x,y;
int id;
point(double x=,double y = ):x(x),y(y) {}
} p[N],q[N];
struct node
{
int u,v;
double w;
} ed[N<<];
int fa[N],g;
double t1[N],t2[N];
point a,b,c,d;
typedef point pointt;
point operator -(point a,point b)
{
return point(a.x-b.x,a.y-b.y);
}
int dcmp(double x)
{
if(fabs(x)<eps) return ;
return x<?-:;
} double dis(point a)
{
return sqrt(a.x*a.x+a.y*a.y);
}
bool cmmp(node ta,node tb)
{
return ta.w<tb.w;
}
int findx(int x)
{
if(x!=fa[x])
fa[x] = findx(fa[x]);
return fa[x];
}
void add(point ta,point tb)
{
ed[++g].u = ta.id;
ed[g].v = tb.id;
ed[g].w = dis(ta-tb);
}
int main()
{
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
int t,i,n,m;
int kk = ;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y);
for(i = ; i <= n+m ; i++)
fa[i] = i;
for(i = ; i < n; i++)
{
scanf("%lf",&t1[i]); // cout<<p[i].x<<" "<<p[i].y<<endl;
}
for(i = ; i< m; i++)
{
scanf("%lf",&t2[i]); }
sort(t1,t1+n);
sort(t2,t2+m);
int h=;
for(i=;i<n;i++)
if(i==n-||t1[i]!=t1[i+])
t1[h++]=t1[i];
n=h;
h=;
for(i=;i<m;i++)
if(i==m-||t2[i]!=t2[i+])
t2[h++]=t2[i];
m=h;
for(i = ; i < n;i++)
{
p[i] = point(a.x*t1[i]+b.x*(-t1[i]),a.y*t1[i]+b.y*(-t1[i]));
p[i].id = i+;
}
for(i = ; i < m ; i++)
{
q[i] = point(c.x*t2[i]+d.x*(-t2[i]),c.y*t2[i]+d.y*(-t2[i]));
q[i].id = i+n+;
}
g = ;
double sum1 = ,sum2 = ;
for(i = ; i < n- ; i++)
{
add(p[i],p[i+]);
sum1+=dis(p[i]-p[i+]);
}
for(i = ; i < m- ; i++)
{
add(q[i],q[i+]);
sum2+=dis(q[i]-q[i+]);
}
if(n==||m==)
{ printf("Case #%d: %.3lf\n",++kk,sum1+sum2);
continue;
}
for(i = ; i < n; i++)
{
// point pp ;
// if(dcmp(c.x-d.x)==0)
// pp = point(c.x,p[i].y);
// else
// pp = dispoint(c,d,p[i]);
// if(dcmp(pp.x-min(c.x,d.x))<=0||dcmp(pp.x-max(c.x,d.x))>=0)
// {
// add(i,n+1);
// if(n+2<n+m)
// add(i,n+2);
// if(n+m-1>n+1)
// add(i,n+m-1);
// add(i,n+m);
// continue;
// }
// int k = find(n+1,n+m,c,dis(pp-c));
// cout<<p[k].id<<" "<<pp.x<<" "<<pp.y<<endl;
// add(i,k);
// if(k!=n+1)
// add(i,k-1);
// if(k!=n+m)
// add(i,k+1);
int l, r;
l = ;
r = m-;
while (r - l > )
{
int mid1 = (r + l) >> ;
int mid2 = (mid1 + r) >> ;
if (dis(p[i]-q[mid1]) > dis(p[i]-q[mid2]))
l = mid1;
else
r = mid2;
}
// int k = find(1,n,a,dis(pp-a));
add(p[i],q[l]);
add(p[i],q[r]);
if(l->=)
add(p[i],q[l-]);
if(r+<=m-)
add(p[i],q[r+]);
}
sort(ed+,ed+g+,cmmp);
int num = ;
double sum = ;
for(i = ; i <= g; i++)
{
int tx = findx(ed[i].u);
int ty = findx(ed[i].v);
if(tx!=ty)
{
fa[tx] = ty;
num++;
sum+=ed[i].w;
}
}
printf("Case #%d: %.3f\n",++kk,sum);
}
return ;
}

hdu3228Island Explorer的更多相关文章

  1. 企业IT管理员IE11升级指南【1】—— Internet Explorer 11增强保护模式 (EPM) 介绍

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  2. 企业IT管理员IE11升级指南【2】—— Internet Explorer 11 对Adobe Flash的支持

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  3. 企业IT管理员IE11升级指南【6】—— Internet Explorer 11面向IT专业人员的常见问题

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  4. windows命令——explorer

    转至http://www.cnblogs.com/ymind/archive/2012/03/30/explorer-command-args.html 今天才知道,explorer原来可以这样用, ...

  5. Unable to extract 64-bitimage. Run Process Explorer from a writeable directory

    Unable to extract 64-bitimage. Run Process Explorer from a writeable directory When we run Process E ...

  6. Google调用explorer.exe打开本地文件

    给IE浏览器地址栏输个本地文件路径,会自动用explorer.exe打开,这个挺好的,但是IE对jQuery稍微高点的版本不怎么待见,只好自己给Google折腾一个调用explorer的功能----- ...

  7. Windows中explorer(图形壳)

    explorer是Windows程序管理器或者文件资源管理器. 用于管理Windows图形壳.(桌面和文件管理.) 删除该程序会导致Windows图形界面无法使用. explorer.exe进程是微软 ...

  8. WebSphere MQ Explorer的sqlserver的jdbc

    一.IBM WebSphere MQ7.0的jdbc支持数据库有: DB2 Informix Informix_With_Date_Format Microsoft_SQL_Server Oracle ...

  9. 更改eclipse的Package Explorer的字体

    说一个牛B的不像实力派的东西 — 更改eclipse的Package Explorer的字体1. 打开eclipse目录/Applications/Eclipse.app/Contents/Eclip ...

随机推荐

  1. xib连线出错,模型保存cell状态(最后个Cell隐藏分割线),

    一个.m文件中有好几个cell类,拖线,要看看该控件对应的是哪个类,否则点击事件不响应,因为归属的xib错了 拖不过来线,因为是view拖不动,加了个button就行了   使用模型属性记录是否隐藏c ...

  2. Elasticsearch基本操作

    ElasticSearch操作说明   活动 方法 url Body 集群的相关操作 查看健康 Get http://localhost:9200/_cluster/health 查看节点 Get h ...

  3. WINFORM 打开PDF

    这里使用 Adobe Read 组件 的方式 首先电脑上需要安装AdobeRead VS中添加COM引用 再在工具选项卡中添加Adobe PDF Read 组件即可 从工具箱中直接拖动组件到窗体中即可

  4. tensorflow 学习(一)

    改系列只为记录我学习 udacity 中深度学习课程!! 1. 整个课程分为四个部分,如上图所示. 第一部分将研究逻辑分类器,随机优化以及实际数据训练. 第二部分我们将学习一个深度网络,和使用正则化技 ...

  5. JSP中的隐式对象(implicit object)

  6. Asp.net导出Excel乱码的解决方法

    通过跟踪Asp.net服务器代码,没有乱码,然而导出Excel到浏览器后,打开时出现乱码. 解决方法是添加编码格式的前缀字节码:Response.BinaryWrite(System.Text.Enc ...

  7. 简要介绍Apache、php、mysql安装和工具介绍

    1 安装Apache 网站:www.Apache.org下载相应的Apache,目前下载了近期的:httpd-2.2.15-win32-x86-openssl-0.9.8msi 安装简要步骤如下图: ...

  8. Java条件编译

    学习过C语言或者C++语言的同学都知道它们支持条件编译,那么今天我们来学习下在Java语言中如何实现条件编译.Java语言本身没有提供条件编译,但是Java编译器对.java文件编译为.class文件 ...

  9. String.format中大括号的加入方法

    因为相对于string Builder  自己更喜欢 string.format 的形式拼接字符串。 今天在写代码的时候怎么都报错,弄的我很奇怪 最后发现问题出在字符串中出现大括号“{”的问题,我想这 ...

  10. HAL驱动库学习--如何使用HAL库

    一 概述 下图是表述了用户层和HAL_Driver的交互过程,基本上从用户APP调用HAL_Driver APIs并且视情况当使用DMA或者专用中断时通过中断处理程序调用HAL_Driver APIs ...