题目描述

Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms.

Each of the N (1 ≤ N ≤ 1,000) farms (conveniently numbered 1..N) is represented by a position (Xi, Yi) on the plane (0 ≤ Xi ≤ 1,000,000; 0 ≤ Yi ≤ 1,000,000). Given the preexisting M roads (1 ≤ M ≤ 1,000) as pairs of connected farms, help Farmer John determine the smallest length of additional roads he must build to connect all his farms.

给出nn个点的坐标,其中一些点已经连通,现在要把所有点连通,求修路的最小长度.

输入输出格式

输入格式:

  • Line 1: Two space-separated integers: N and M

  • Lines 2..N+1: Two space-separated integers: Xi and Yi

  • Lines N+2..N+M+2: Two space-separated integers: i and j, indicating that there is already a road connecting the farm i and farm j.

输出格式:

  • Line 1: Smallest length of additional roads required to connect all farms, printed without rounding to two decimal places. Be sure to calculate distances as 64-bit floating point numbers.

输入输出样例

输入样例#1:

4 1
1 1
3 1
2 3
4 3
1 4
输出样例#1:

4.00
 
 

裸kruskal

屠龙宝刀点击就送

#include <algorithm>
#include <cstdio>
#include <cmath>
#define N 1000005
typedef long long LL;
using namespace std;
int cnt,fa[],n,m,q;
LL x[],y[];
struct Edge
{
int x,y;
double dist;
bool operator<(Edge a)const
{
return dist<a.dist;
}
}edge[N];
int find_(int x) {return x==fa[x]?x:fa[x]=find_(fa[x]);}
double calc(LL x1,LL y1,LL x2,LL y2) {return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i)
{
fa[i]=i;
scanf("%lld%lld",&x[i],&y[i]);
}
for(int u,v,i=;i<=m;++i)
{
scanf("%d%d",&u,&v);
fa[find_(v)]=find_(u);
}
for(int i=;i<=n;++i)
for(int j=i+;j<=n;++j)
edge[++cnt]=(Edge){i,j,calc(x[i],y[i],x[j],y[j])};
sort(edge+,edge++cnt);
double sum=;
for(int num=,i=;i<=cnt;++i)
{
int fx=find_(edge[i].x),fy=find_(edge[i].y);
if(fx!=fy)
{
fa[fy]=fx;
sum+=edge[i].dist;
if(++num==n-) break;
}
}
printf("%.2lf",sum);
return ;
}

洛谷 P2872 [USACO07DEC]道路建设Building Roads的更多相关文章

  1. 洛谷——P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

  2. 洛谷 P2872 [USACO07DEC]道路建设Building Roads 题解

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

  3. bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads kruskal求最小生成树. #include<iostream> #include<cstdio> ...

  4. $P2872\ [USACO07DEC]道路建设Building\ Roads$

    \(problem\) 错的原因是\(RE\)(大雾 , 时刻谨记 \(N\) 个地方的话 保守开 \(\frac{N^2}{2}\) 大小. 因为是边. 边最多的情况即完全图 : $1+2+3+4. ...

  5. [USACO07DEC]道路建设Building Roads

    题目:洛谷P2872.POJ3625. 题目大意:给你n个点的坐标,有些点已经有边连通,现在要你连上剩下的所有点,求这些边的最小长度是多少(不包括原来的边). 解题思路:最小生成树,把所有边处理出来, ...

  6. 洛谷 P2872 【[USACO07DEC]道路建设Building Roads】

    P2872 传送门 首先 题目概括:题目让着求使所有牧场都联通.需要修建多长的路. 显然这是一道最小生成树板子题(推荐初学者做). 那我就说一下kruskal吧. Kruskal算法是一种用来查找最小 ...

  7. 题解 P2872 【[USACO07DEC]道路建设Building Roads】

    这道题真的是令人窒息,Kruskal调了贼久一直RE,最后发现数组大小稍微少了那么一点点.(也就10倍吧..) 言归正传,根据本人的分析(以及算法标签的提示),这是一道求最小生成树的题目,当然要注意已 ...

  8. USACO 07DEC 道路建设(Building Roads)

    Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he ...

  9. 洛谷 P5019 铺设道路

    题目描述 春春是一名道路工程师,负责铺设一条长度为 \(n\) 的道路. 铺设道路的主要工作是填平下陷的地表.整段道路可以看作是 \(n\) 块首尾相连的区域,一开始,第 \(i\) 块区域下陷的深度 ...

随机推荐

  1. 【转】Cache Buffer Chain 第三篇

    文章转自:http://oracle.chinaitlab.com/induction/862509.html,文章前部分转载,后部分自己加上的. Oracle数据库只读模式的CACHE BUFFER ...

  2. Unity4.0配置

    关于Unity4.0的使用: 一 安装Unity 在程序包管理器控制台输入命令:Istall-Pckage unity.mvc安装后会在App_Start中生成UnityConfig.cs 和Unit ...

  3. Summit Online Judge

    题意: 询问将取值在 $[L,R]$ 的若干个整数相加,可以得到 $[x,y]$ 区间内多少个数字. 解法: 只需要考虑求 $[L,R]$ 的数字能凑出 $[1,n]$ 的多少个数字,即可得出答案. ...

  4. 1.SJ-SLAM-14

    1.引言 SLAM:Simultaneous Localization and Mapping 同时定位与地图构建 搭载特定传感器的主体,在没有环境先验信息的情况下,于运动过程中建立环境的模型,同时估 ...

  5. codeforces 813C The Tag Game 树+dfs追击问题

    C. The Tag Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutpu ...

  6. 668. Kth Smallest Number in Multiplication Table

    Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...

  7. Mybatis:resultMap的使用总结(转自https://www.cnblogs.com/kenhome/p/7764398.html)

    resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中. resultMap包含的元素: <!--column不做限制,可以为任意 ...

  8. andriod ndk配置

    r7及以后版本不需要再配置cywin ,留影 先在path中配置ndk的环境变量,位置为ndk的根目录,也可在图5中的位置配置环境变量

  9. [Xcode 实际操作]九、实用进阶-(9)陀螺仪设备的使用

    目录:[Swift]Xcode实际操作 本文将演示陀螺仪设备的使用. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //导入需要用到的C ...

  10. [Xcode 实际操作]九、实用进阶-(29)为App添加IAP(支付方式)内购项目

    目录:[Swift]Xcode实际操作 首先请阅读:[Xcode10 实际操作]九.实用进阶-(28)在iTunes Connect(苹果商店的管理后台)中创建一个新的新的APP 本文将演示如何给刚刚 ...