HDU 3228 题解(最小生成树)(Kruskal)(内有详细注释)
Problem Description
A group of explorers has found a solitary island. They land on the island and explore it along a straight line. They build a lot of campsites while they advance. So the campsites are laid on the line.
Coincidently, another group of explorers land on the island at the same time. They also build several campsites along another straight line. Now the explorers meet at the island and they decide to connect all the campsites with telegraph line so that they can communicate with each other wherever they are.
Simply building segments that connect a campsite to another is quite easy, but the telegraph line is rare. So they decide to connect all the campsites with as less telegraph line as possible. Two campsites are connected if they are directly connected with telegraph line or they are both connected to another campsite.
Input
There are multiple test cases.
The number of the test cases is in the first line of the input.
For each test case, first line contains two integers N and M (0≤N, M≤10000), which N is the number of the campsites of the first group of explorers and M is the number of the campsites of the second group of explorers. And there exist at least one campsite.
The next two lines contain eight integers Ax, Ay, Bx, By, Cx, Cy, Dx, Dy. Their absolute values are less than 1000. The integers are the coordinates of four points A, B, C and D. The exploring path of the first group is begin with the first point A and end with the second point B, and the path of the second group is from the third point C to the fourth point D. Every pair of points is distinct.
The last two lines of the test case contain N and M real numbers; they indicate the positions of the campsites. Suppose the i-th real number in the first line is t. It means the x-coordinate of the i-th campsite is Ax * t + Bx * (1-t), and the y-coordinate is Ay * t + By * (1-t). Equally, the campsite on the second straight line is C * t + D * (1-t). You can assume that there are at most four digits in the decimal part, and the numbers are always between 0 and 1.
Output
For each test case, output contains only a real number rounded to 0.001.
Sample Input
1
4 4
0 0 10 10
0 10 10 0
0.1 0.3 0.6 0.8
0.1 0.3 0.6 0.8
Sample Output
Case #1: 19.638
分析:
此题是一道最小生成树的模板题,但题目较复杂,我们采用分步的方法处理问题
1.处理点
为了处理点的方便,我们可以编写结构体,使点的处理更简洁。还有一点细节,一条直线上的同一个位置可能会有多个营地,所以要判重
struct point {
double x;//x坐标
double y;//y坐标
int id;//点的编号(之后要用)
} p[MAXN],q[MAXN];
point make_point(double x,double y,int id) {//初始化
point tmp;
tmp.x=x;
tmp.y=y;
tmp.id=id;
return tmp;
}
void input(point* a) {//输入
scanf("%lf%lf",&(a->x),&(a->y));
}
double getdis(point a,point b) {//两点之间距离公式
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
2.建图
时间方面:如果我们把每个点和其他所有点都连起来,时间复杂度是\(O((n+m)^2)\),肯定是不行的,因此我们先将同一条直线上的每一个点连起来,然后对于一条直线上的点,三分出距离另一条直线最近的两个点进行连边。为了防止特殊情况,也把这两个点旁边的两个点连接起来。
int l,r;
l=0;
r=m-1;
while(r-l>1) {//三分过程
int mid1=(r+l)/2;
int mid2=(mid1+r)/2;
if(getdis(p[i],q[mid1])>getdis(p[i],q[mid2])) l=mid1;
else r=mid2;
}
空间方面:用邻接矩阵的话空间会超出限制,于是采用邻接表进行存储
struct edge_table {
int from;//起点
int to;//终点
double value;//长度
} edge[MAXN*32];
int edge_cnt=0;//数边的条数
void add_edge(int u,int v,double w) {
edge[++edge_cnt].from=u;
edge[edge_cnt].to=v;
edge[edge_cnt].value=w;
}
3.最小生成树
笔者一开始使用的是堆优化的prim算法,理论上时间复杂度应为\(O(nlog_2n)\),但不知道由于什么原因会TLE,使用Kruskal算法则可AC
TLE代码:
struct edge_table {
int from;
int to;
//int next;
double value;
} edge[MAXN*32];
int head[MAXN+MAXN];
int edge_cnt=0;
void add_edge(int u,int v,double w) {
edge[++edge_cnt].from=u;
edge[edge_cnt].to=v;
edge[edge_cnt].value=w;
edge[edge_cnt].next=head[u];
head[u]=edge_cnt;
}
struct heap_node{//建堆
int id;
double value;
friend bool operator <(heap_node a,heap_node b){
return a.value>b.value;
}
};
double key[MAXN+MAXN];
int used[MAXN+MAXN];
double prim(){
double ans=0;
int tot=0;
memset(key,0x7f,sizeof(key));
memset(used,0,sizeof(used));
priority_queue<heap_node>heap;
heap_node now,nex;
now.id=1;
now.value=key[1]=0;
heap.push(now);
while(!heap.empty()){
now=heap.top();
heap.pop();
int u=now.id;
if(now.value!=key[u]) continue;
used[u]=1;
ans+=key[u];
tot++;
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].to;
if(used[v]==0&&key[v]>edge[i].value){
key[v]=edge[i].value;
nex.value=key[v];
nex.id=v;
heap.push(nex);
}
}
}
if(tot<n+m) ans=-1;
return ans;
}
AC代码:
int father[MAXN];//建立并查集
int find(int x) {//并查集的查找函数
if(father[x]!=x) father[x]=find(father[x]);//路径压缩
return father[x];
}
int comp(edge_table a,edge_table b) {//按边的长度从小到大排序
return a.value<b.value;
}
double kruskal() {
for(int i=0; i<=n+m; i++) father[i]=i;//初始化并查集,让每一个点自成一个独立的连通分量
double sum=0;
sort(edge+1,edge+edge_cnt+1,comp);//按边的长度从小到大排序
for(int i=1; i<=edge_cnt; i++) {
int tx=find(edge[i].from);
int ty=find(edge[i].to);
if(tx!=ty) {//如果两个点在两个不同的连通分量
father[tx]=ty;//合并两个连通分量
sum+=edge[i].value;//将边加入最小生成树
}
}
return sum;
}
代码:
全部代码如下:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define MAXN 50005
using namespace std;
struct point {
double x;//x坐标
double y;//y坐标
int id;//点的编号(之后要用)
} p[MAXN],q[MAXN];
point make_point(double x,double y,int id) {//初始化
point tmp;
tmp.x=x;
tmp.y=y;
tmp.id=id;
return tmp;
}
void input(point* a) {//输入
scanf("%lf%lf",&(a->x),&(a->y));
}
double getdis(point a,point b) {//两点之间距离公式
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
struct edge_table {
int from;//起点
int to;//终点
double value;//长度
} edge[MAXN*32];
int edge_cnt=0;//数边的条数
void add_edge(int u,int v,double w) {
edge[++edge_cnt].from=u;
edge[edge_cnt].to=v;
edge[edge_cnt].value=w;
}
int n,m;
double t1[MAXN],t2[MAXN];
void delete_same() {//判重函数
sort(t1,t1+n);
sort(t2,t2+m);
int ptr=0;
for(int i=0; i<n; i++) { //判重过程,可在纸上模拟,方便理解
if(i==n-1||t1[i]!=t1[i+1]) t1[ptr++]=t1[i];
}
n=ptr;//将n重置为判重后点的数目
ptr=0;
for(int i=0; i<m; i++) {
if(i==m-1||t2[i]!=t2[i+1]) t2[ptr++]=t2[i];
}
m=ptr;
}
int father[MAXN];//建立并查集
int find(int x) {//并查集的查找函数
if(father[x]!=x) father[x]=find(father[x]);//路径压缩
return father[x];
}
int comp(edge_table a,edge_table b) {//按边的长度从小到大排序
return a.value<b.value;
}
double kruskal() {
for(int i=0; i<=n+m; i++) father[i]=i;//初始化并查集,让每一个点自成一个独立的连通分量
double sum=0;
sort(edge+1,edge+edge_cnt+1,comp);//按边的长度从小到大排序
for(int i=1; i<=edge_cnt; i++) {
int tx=find(edge[i].from);
int ty=find(edge[i].to);
if(tx!=ty) {//如果两个点在两个不同的连通分量
father[tx]=ty;//合并两个连通分量
sum+=edge[i].value;//将边加入最小生成树
}
}
return sum;
}
int main() {
point a,b,c,d;
int cnt;
scanf("%d",&cnt);
for(int cas=1; cas<=cnt; cas++) {
int i;
scanf("%d %d",&n,&m);
input(&a);
input(&b);
input(&c);
input(&d);
for(i=0; i<n; i++) scanf("%lf",&t1[i]);
for(i=0; i<m; i++) scanf("%lf",&t2[i]);
delete_same();
for(i=0; i<n; i++) {//将点加入
p[i]=make_point(a.x*t1[i]+b.x*(1-t1[i]),a.y*t1[i]+b.y*(1-t1[i]),i+1);
}
for(i = 0 ; i < m ; i++) {
q[i]=make_point(c.x*t2[i]+d.x*(1-t2[i]),c.y*t2[i]+d.y*(1-t2[i]),i+n+1);
}
edge_cnt=0;
double sum1=0,sum2=0;
for(i=0; i<n-1; i++) {//将每条直线上的点连起来
double l=getdis(p[i],p[i+1]);
add_edge(p[i].id,p[i+1].id,l);//由于是无向图,每条边存两遍
add_edge(p[i+1].id,p[i].id,l);
sum1+=l;
}
for(i=0; i<m-1; i++) {
double l=getdis(q[i],q[i+1]);
add_edge(q[i].id,q[i+1].id,l);
add_edge(q[i+1].id,q[i].id,l);
sum2+=l;
}
if(n==0||m==0) {//如果只有一条直线的特判
printf("Case #%d: %.3lf\n",cas,sum1+sum2);
continue;
}
for(int i=0; i<n; i++) {
int l,r;
l=0;
r=m-1;
while(r-l>1) {//三分过程
int mid1=(r+l)/2;
int mid2=(mid1+r)/2;
if(getdis(p[i],q[mid1])>getdis(p[i],q[mid2])) l=mid1;
else r=mid2;
}
add_edge(p[i].id,q[l].id,getdis(p[i],q[l]));//将距离最短的两条边加入邻接表
add_edge(q[l].id,p[i].id,getdis(p[i],q[l]));
add_edge(p[i].id,q[r].id,getdis(p[i],q[r]));
add_edge(q[r].id,p[i].id,getdis(p[i],q[r]));
if(l-1>=0) {//将距离第二短的两条边加入邻接表
add_edge(p[i].id,q[l-1].id,getdis(p[i],q[l-1]));
add_edge(q[l-1].id,p[i].id,getdis(p[i],q[l-1]));
}
if(r+1<=m-1) {
add_edge(p[i].id,q[r+1].id,getdis(p[i],q[r+1]));
add_edge(q[r+1].id,p[i].id,getdis(p[i],q[r+1]));
}
}
double sum=kruskal();
printf("Case #%d: %.3lf\n",cas,sum);
}
}
HDU 3228 题解(最小生成树)(Kruskal)(内有详细注释)的更多相关文章
- HDU 4786(最小生成树 kruskal)
题目链接:pid=4786" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=4786 Prob ...
- DirectShow中写push模式的source filter流程 + 源代码(内附详细注释)
虽然网上已有很多关于DirectShow写source filter的资料,不过很多刚开始学的朋友总说讲的不是很清楚(可能其中作者省略了许多他认为简 单的过程),读者总希望看到象第一步怎么做,第二步怎 ...
- 详细注释!二维码条码扫描源码,使用Zxing core2.3
from:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=325529&page=1 现如今很多项目中都会应用到条码扫描解 ...
- 最小生成树——Kruskal算法理解
背景:本文是在小甲鱼数据结构教学视频中的代码的基础上,添加详细注释而完成的.该段代码并不完整,仅摘录了核心算法部分,结合自己的思考,谈谈理解. Prim算法理解: 如图(摘录自小甲鱼教学视频中的图片) ...
- 【转】最小生成树——Kruskal算法
[转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...
- hdu Constructing Roads (最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...
- 模板——最小生成树kruskal算法+并查集数据结构
并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...
- 最小生成树——Kruskal与Prim算法
最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个 ...
- [APIO2019] [LOJ 3145] 桥梁(分块+并查集)(有详细注释)
[APIO2019] [LOJ 3145] 桥梁(分块+并查集)(有详细注释) 题面 略 分析 考试的时候就感觉子任务4是突破口,结果却写了个Kruskal重构树,然后一直想怎么在线用数据结构维护 实 ...
随机推荐
- linux 时区问题
1.java项目发现 服务器时间不正确,修改了服务器时间之后依然没解决. 2.java虚拟机的时区也需要设置:
- 全文索引:部分词能查到,部分词查不到的bug
全文索引的概念:将存储在数据库中的整本书或整篇文章中的任意内容信息查找出来的技术.它可以根据需要获取全文中有关章,节,段,句,词等信息,也可以进行各种统计和分析. 原理是先定义一个词库,然后在文章中查 ...
- ubuntu重装--备份/配置
https://github.com/wenlin-gk/document/blob/master/ubuntu%E5%A4%87%E4%BB%BD%2B%E9%85%8D%E7%BD%AE.txt
- 【NOIP2016提高A组模拟8.14】疯狂的火神
题目 火神为了检验zone的力量,他决定单挑n个人. 由于火神训练时间有限,最多只有t分钟,所以他可以选择一部分人来单挑,由于有丽子的帮助,他得到了每个人特定的价值,每个人的价值由一个三元组(a,b, ...
- 【NOIP2013模拟】归途与征程
题目 分析 好吧...明显是暴力题. 首先,把A串分成只有小写字母组成的小分串,按顺序存放:A[1].A[2].A[3]--. 对于同构循环串,显然把两个B串合在一起,成为一个新的C串.\(C[i.. ...
- Java——super
在Java类中使用super来引用基类的成分. [代码]
- Anaconda-navigator 打不开的解决方法(亲测有效!)
本文链接:https://blog.csdn.net/qq_42489092/article/details/92208822method_1:每当你打不开应用时,不妨试一下:用管理员身份运行 请用管 ...
- AC自动机再加强版
AC自动机 拓扑排序优化,注意拓扑排序前要把所有入度为零的点都加进去 #include<bits/stdc++.h> using namespace std; #define maxn 1 ...
- 【转】DataRow复制一行到另一个DataTable
源地址:http://www.cnblogs.com/pains/archive/2007/11/22/969003.html 下面两个方法是DataRow复制一行到另一个DataTable的, ...
- Python编程:从入门到实践—if 语句
Python编程:从入门到实践—if 语句