hdu3228Island Explorer
给你两条线及两条线上的点,求最小生成树。
可以挨个枚举一条线上的点,三分出另一条线上离他最近的点进行连边。
注意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的更多相关文章
- 企业IT管理员IE11升级指南【1】—— Internet Explorer 11增强保护模式 (EPM) 介绍
企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...
- 企业IT管理员IE11升级指南【2】—— Internet Explorer 11 对Adobe Flash的支持
企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...
- 企业IT管理员IE11升级指南【6】—— Internet Explorer 11面向IT专业人员的常见问题
企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...
- windows命令——explorer
转至http://www.cnblogs.com/ymind/archive/2012/03/30/explorer-command-args.html 今天才知道,explorer原来可以这样用, ...
- 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 ...
- Google调用explorer.exe打开本地文件
给IE浏览器地址栏输个本地文件路径,会自动用explorer.exe打开,这个挺好的,但是IE对jQuery稍微高点的版本不怎么待见,只好自己给Google折腾一个调用explorer的功能----- ...
- Windows中explorer(图形壳)
explorer是Windows程序管理器或者文件资源管理器. 用于管理Windows图形壳.(桌面和文件管理.) 删除该程序会导致Windows图形界面无法使用. explorer.exe进程是微软 ...
- WebSphere MQ Explorer的sqlserver的jdbc
一.IBM WebSphere MQ7.0的jdbc支持数据库有: DB2 Informix Informix_With_Date_Format Microsoft_SQL_Server Oracle ...
- 更改eclipse的Package Explorer的字体
说一个牛B的不像实力派的东西 — 更改eclipse的Package Explorer的字体1. 打开eclipse目录/Applications/Eclipse.app/Contents/Eclip ...
随机推荐
- 20145209&20145309信息安全系统设计基础实验报告 (3)
实验内容.步骤与体会: 实验过程的理解,实验指导书中知识点的理解. (1)为什么在双击了GIVEIO和JTAG驱动安装文件后还需要手动进行配置? 因为安装文件只是将驱动文件释放了出来,并没有在系统中将 ...
- 用GSON解析Json格式数据
GSON是谷歌提供的开源库,用来解析Json格式的数据,非常好用.如果要使用GSON的话,则要先下载gson-2.2.4.jar这个文件,如果是在Android项目中使用,则在Android项目的li ...
- HTML添加多媒体或音乐
1,添加多媒体 <embed src="多媒体文件地址" width="多媒体的宽度" height="多媒体的高度" autosta ...
- asp.net页面间传值方式
使用asp.net开发项目,必然会在页面间进行传值,本文介绍几种常见的页面传值方式,仅作笔记,以便后续查找使用. 前提:新建两个页面:ValuePage.aspx,ObtainValue.aspx,本 ...
- Java中的集合框架
概念与作用 集合概念 现实生活中:很多事物凑在一起 数学中的集合:具有共同属性的事物的总体 java中的集合类:是一种工具类,就像是容器,储存任意数量的具有共同属性的对象 在编程时,常常需要集中存放多 ...
- Hausdorff distance
微分动力系统原理 这本书里有介绍 Hausdorff距离是描述两组点集之间相似程度的一种量度,它是两个点集之间距离的一种定义形式:假设有两组集合A={a1,…,ap},B={b1,…,bq},则这两个 ...
- javascript对json对象的序列化与反序列化
首先引入一个json2.js.官方的地址为:https://github.com/douglascrockford/JSON-js 这里为了方便我直接贴上源代码 /* json2.js 2013-05 ...
- Android APK反编译详解(附图)
转载自http://blog.csdn.net/sunboy_2050/article/details/6727581 这段时间在学Android应用开发,在想既然是用Java开发的应该很好反编译从而 ...
- Java关于md5加密
package com.mi.util; /** * md5+salt 长度为32的加密 * @author admin * */ public class MD5 { public static v ...
- Android Studio 入门指南
转载: 原文链接:http://www.codeceo.com/article/android-studio-guide.html 写在前面 作为一个Android 开发者,你应该很了解Android ...