题目描述

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. CF-805A

    A. Fake NP time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  2. springMVC拦截配置

    1.web.xml文件配置 <!-- spring mvc --> <servlet> <servlet-name>DispatcherServlet</se ...

  3. 用css写三角形,宽高可设置

    1.不传@h,@c === @h; 2.元素width = @w, 元素height = @h*2 3.配合上.center()实现图标居中 less版本: //上下左右居中 .center(){ p ...

  4. Bloomberg Desktop Api 关于历史Tick数据的一些参考

    使用WAPI命令可以看到当前BBG的Api情况. 目前2016年3月是V3的版本.其中有一个API Develper's Guide 中有Core Develper Guide的pdf, 里面提到了B ...

  5. CZGL.AliIoTClient 文档:说明

    文档目录: 说明 1. 连接阿里云物联网 2. IoT 客户端 3. 订阅Topic与响应Topic 4. 设备上报属性 4.1 上报位置信息 5. 设置设备属性 6. 设备事件上报 7. 服务调用 ...

  6. Python 实现简单的登录注册界面

    Python 实现简单的登录注册界面 注意:编写代码之前需要导入很重要的包 import tkinter as tk import pickle from tkinter import message ...

  7. LVS-DR VIP和RIP不同网段的配置方法

    http://blog.itpub.net/25723371/viewspace-1446935/

  8. adb server version (39) doesn't match this client (40); killing...

    在启动RN项目的时候也报错,上面的错误是在adb的环境变量中的位置和android studio的sdk不是一个位置.adb是在sdk中的,所以他们应该是一致的位置 android studio的sd ...

  9. 推荐一款功能齐全的开源客户端( iOS 、Android )研发助手。

    DoraemonKit ,简称DoKit,中文名 哆啦A梦,意味着能够像哆啦A梦一样提供给他的主人各种各样的工具. 开发背景 每一个稍微有点规模的 App,总会自带一些线下的测试功能代码,比如环境切换 ...

  10. hdu 2108 Shape of HDU(判定是不是凸多边形)

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...