A - Treehouses Kattis - treehouses--分块并查集

In a rainforest there are n treehouses high in the forest canopy on different trees (numbered from 1 to n). The i-th tree’s location is at (xi,yi). The first e of them in the list are close enough to neighboring open land around the rainforest so that transportation between all of them is easy by foot. Some treehouses may already be connected by direct straight cables through the air that can allow transport between them.

Residents want easy transportation between all the treehouses and the open land, by some combination of walking (between those near the open land), and using one or more cables between treehouses. This may require the addition of more cables. Since the cables are expensive, they would like to add the smallest possible length of cable.

The height of a cable up two trees can be set so cables can criss-cross other cables, and not allow any snags or crashes. It is not safe to try to switch between two criss-crossed cables in mid-air!

Input
The input will start with the three integers n (1≤n≤1000), e (1≤e≤n), and p (0≤p≤1000), where p is the number of cables in place already.

Next come n lines, each with two real numbers x and y (|x|,|y|≤10000) giving the location of a treehouse. The i-th coordinate pair is for the treehouse with ID i. All coordinate pairs are unique. Real numbers are stated as integers or include one digit after a decimal point.

Next come p lines, each with two integers a, b, where 1≤a<b≤n, giving the two treehouse ids of an existing cable between their trees. No ID pair will be repeated.

Output
The output is the minimum total length of new cable that achieves the connection goal, expressed with absolute or relative error less than 0.001.

Sample Input 1 Sample Output 1
3 1 0
0.0 0.0
2.0 0.0
1.0 2.0
4.236067
Sample Input 2 Sample Output 2
3 1 1
0.0 0.0
0.5 2.0
2.5 2.0
1 2
2.000000
Sample Input 3 Sample Output 3
3 2 0
0.0 0.0
2.0 0.0
1.0 2.0
2.236067

题目意思:
给你一些点的坐标,已知前e个点不用连,已知中间有p对点不用连,求用最少的距离的线,将全部的点连起来;

#include<bits/stdc++.h>
using namespace std;
double x[1005];
double y[1005];
int fa[1005];
int getf(int x)
{
    if(x==fa[x]) return x;
    return fa[x]=getf(fa[x]);
}
struct node{
    int u,v;
    double w;
}e[1000021];
int edn;
bool cmp(node x,node y)
{
    return x.w<y.w;
}
int main()
{
    int n,ee,p;
    cin>>n>>ee>>p;
    for(int i=1;i<=n;i++)
    {
        fa[i]=i;
        cin>>x[i]>>y[i];
    }
    for(int i=2;i<=ee;i++)
    {
        fa[getf(i)]=getf(1);
    }
    for(int i=1,u,v;i<=p;i++)
    {
        cin>>u>>v;
        fa[getf(u)]=getf(v);
    }
    for(int i=1;i<=n;i++)//将任意两点的距离存下来
    {
        for(int j=1+i;j<=n;j++)
        {
            e[++edn]=(node){getf(i),getf(j),sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2))};
        }
    }
    sort(e+1,e+1+edn,cmp);
    double ans=0;
    for(int i=1;i<=edn;i++)
    {
        int u=e[i].u;
        int v=e[i].v;
        if(getf(u)==getf(v)) continue;
        ans+=e[i].w;
        fa[getf(u)]=getf(v);
    }
    printf("%.6f\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JdiLfc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值