P2872 [USACO07DEC]道路建设Building Roads

题目描述

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

最小生成树裸题
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 510000
using namespace std;
int n,m,x,y,s,fx,fy,xx[N],yy[N],fa[N];
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
struct Edge
{
    int x,y;
    double z;
}edge[N<<];
int cmp(Edge a,Edge b)
{
    return a.z<b.z;
}
int find(int x)
{
    if(fa[x]==x) return x;
    fa[x]=find(fa[x]);
    return fa[x];
}
int main()
{
    n=read(),m=read();
    ;i<=n;i++)
     xx[i]=read(),yy[i]=read();
    ;i<=n;i++)
     ;j<=n;j++)
      if(i!=j)
      {
           ++s;
           edge[s].x=i;
           edge[s].y=j;
           edge[s].z=sqrt(pow(xx[i]-xx[j],)+pow(yy[i]-yy[j],));
      }
    ;i<=n;i++) fa[i]=i;
    ;
    ;i<=m;i++)
    {
        x=read(),y=read();
        fx=find(x),fy=find(y);
        fa[fx]=fy;
    }
    sort(edge+,edge++s,cmp);
    ;i<=s;i++)
    {
        x=edge[i].x,y=edge[i].y;
        fx=find(x),fy=find(y);
        if(fa[fx]==fy) continue;
        fa[fx]=fy;
        ans+=edge[i].z;
    }
    printf("%.2lf",ans);
    ;
}

洛谷——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

    题目描述 Farmer John had just acquired several new farms! He wants to connect the farms with roads so th ...

  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. IOS之UIStepper控件详解

    在iOS5中新增了一个数字输入控件UIStepper,它可以递进式输入数量.UIStepper继承自UIControl,它主要的事件是UIControlEventValueChanged,每当它的值改 ...

  2. springboot的多个配置文件的关系

    一般我们在使用springboot时会用到多个环境下的配置文件,例如 application-dev.yml:开发环境 application-uat.yml:用户验收测试环境 application ...

  3. CocosCreator工程内的命名

    命名结构总体的命名结构遵循以下格式 前缀-内容-尾缀 - 前缀:用来定义node的属性- 内容:node的名字- 尾缀:序列或状态1231. 前缀说明:在开始的时候定义/声明这个节点的属性前缀可以是一 ...

  4. vue render {} 对象 说明文档

    Vue学习笔记进阶篇——Render函数 http://www.mamicode.com/info-detail-1906336.html 深入data object参数 有一件事要注意:正如在模板语 ...

  5. docker 深入理解之cgroups

    cgroups 资源限制 cgroups 是什么 cgroups 最初名为process container,有Google工程师Paul Menage和Rohit Seth于 2006 年提出,后由 ...

  6. JavaEE-01 JSP动态网页基础

    学习要点 B/S架构的基本概念 Web项目的创建和运行 JSP页面元素 MyEclipse创建和运行Web项目 Web程序调试 Web简史 web前端技术演进三阶段 WEB 1.0:信息广播. WEB ...

  7. windwos .bat脚本大全

    记录一个很有用比较全面的windows .bat脚本网站 https://www.cnblogs.com/zhaoqingqing/p/4620402.html

  8. 第1节 flume:8、flume采集某个文件内容到hdfs上

    2.         采集文件内容到HDFS 需求分析: 采集需求:比如业务系统使用log4j生成的日志,日志内容不断增加,需要把追加到日志文件中的数据实时采集到hdfs. 同一个日志文件的内容不断增 ...

  9. C++ Simple Message/Logging Class

    在 Qt的源码与Protobuf 的代码中,看到相同的简单消息(日志)输出的类实现,基本思路是使用宏定义,重载临时类对象,调用类方法或者通过析构函数自动调用输出方法,实现消息输出.这里以 Protob ...

  10. [LUOGU] P3004 [USACO10DEC]宝箱Treasure Chest

    第一眼:区间DP,可以瞎搞 f[i][j]=max(sum(i,j)-f[i+1][j],sum(i,j)-f[i][j-1]) 提出来就是f[i][j]=sum(i,j)-min(f[i+1][j] ...