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);
    }
}

猜你喜欢

转载自www.cnblogs.com/birchtree/p/9858080.html
今日推荐