题目描述

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. nginx proxy https

    server {listen 443;server_name mail.jb51.net; ssl on;ssl_certificate server.crt;ssl_certificate_key ...

  2. Levko and Array

    题意: 有一长度为n的正整数序列,你可以选择K个数字任意改变它,使得$max \{ a(i+1) - a(i) \} $ 最小,求最小值. 解法: 1.$O(n^2log(MAX_A) )$,考虑二分 ...

  3. HDOJ-2045

    不容易系列之(3)—— LELE的RPG难题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  4. jQuery获取table当前所在行

    $("div tbody tr").click(function() {         var rows = $(this).prevAll().length + 1;//行号  ...

  5. C#项目中一些文件类型说明

    designer.cs  是窗体设计器生成的代码文件,作用是对窗体上的控件做初始化工作 (在函数InitializeComponent()中)VS2003以前都把这部分代码放到窗体的cs文件中,由于这 ...

  6. Solve Tree Problems Recursively

    "Top-down" Solution Here is the pseudocode for the recursion function maximum_depth(root, ...

  7. 调用Web API将文件上传到服务器的方法(.Net Core)

    最近遇到一个将Excel通过Web API存到服务器的问题,其中涉及到Excel的读取.调用API.Web  API怎么进行接收. 一. Excel的读取.调用API Excel读取以及调用API的代 ...

  8. XHTML学习笔记 Part2:核心元素

    1. <html>元素 <html xmlns="http://www.w3.org/1999/xhtml"> 仅有两个元素是<html>的直接 ...

  9. C笔记列表

    笔记列表 指针是一个变量,其值为另一个变量的地址,即,内存位置的直接地址.就像其他变量或常量一样,您必须在使用指针存储其他变量地址之前,对其进行声明. 要理解指针就要先理解计算机的内存.计算机内存会被 ...

  10. python操作rabbitmq实现广播效果

    生产方(Fanout_Publisher.py) # __author__ = 'STEVEN' import pika #开启socket connection = pika.BlockingCon ...