洛谷——P2872 [USACO07DEC]道路建设Building Roads
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.
输入输出样例
4 1 1 1 3 1 2 3 4 3 1 4
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的更多相关文章
- 洛谷 P2872 [USACO07DEC]道路建设Building Roads 题解
P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...
- 洛谷 P2872 [USACO07DEC]道路建设Building Roads
题目描述 Farmer John had just acquired several new farms! He wants to connect the farms with roads so th ...
- bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads
P2872 [USACO07DEC]道路建设Building Roads kruskal求最小生成树. #include<iostream> #include<cstdio> ...
- $P2872\ [USACO07DEC]道路建设Building\ Roads$
\(problem\) 错的原因是\(RE\)(大雾 , 时刻谨记 \(N\) 个地方的话 保守开 \(\frac{N^2}{2}\) 大小. 因为是边. 边最多的情况即完全图 : $1+2+3+4. ...
- [USACO07DEC]道路建设Building Roads
题目:洛谷P2872.POJ3625. 题目大意:给你n个点的坐标,有些点已经有边连通,现在要你连上剩下的所有点,求这些边的最小长度是多少(不包括原来的边). 解题思路:最小生成树,把所有边处理出来, ...
- 洛谷 P2872 【[USACO07DEC]道路建设Building Roads】
P2872 传送门 首先 题目概括:题目让着求使所有牧场都联通.需要修建多长的路. 显然这是一道最小生成树板子题(推荐初学者做). 那我就说一下kruskal吧. Kruskal算法是一种用来查找最小 ...
- 题解 P2872 【[USACO07DEC]道路建设Building Roads】
这道题真的是令人窒息,Kruskal调了贼久一直RE,最后发现数组大小稍微少了那么一点点.(也就10倍吧..) 言归正传,根据本人的分析(以及算法标签的提示),这是一道求最小生成树的题目,当然要注意已 ...
- USACO 07DEC 道路建设(Building Roads)
Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he ...
- 洛谷 P5019 铺设道路
题目描述 春春是一名道路工程师,负责铺设一条长度为 \(n\) 的道路. 铺设道路的主要工作是填平下陷的地表.整段道路可以看作是 \(n\) 块首尾相连的区域,一开始,第 \(i\) 块区域下陷的深度 ...
随机推荐
- PHP获取时间总结
查询前一年时间戳 mktime(0,0,0,date('m'),date('d'),date('Y')-1); strtotime('-12 month'); 查询前6个月时间戳 mktime(0,0 ...
- TFS2010单独安装配置tfs build server
记录一下确实很磨人. 同样硬件和软件环境的两台服务器,其中一台服务器很久之前就配置好了tfs2010 build ,然后最近想再配置一台tfs build server,但是按照以前的配置流程始终提示 ...
- 初探ABP--记一些常见的开发问题
1.Update-Database : 无法将“Update-Database”项识别为 cmdlet.函数.脚本文件或可运行程序的名称.请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次. ...
- 洛谷 P1548 棋盘问题
题目描述 设有一个N*M方格的棋盘(l<=N<=100,1<=M<=100)(30%) 求出该棋盘中包含有多少个正方形.多少个长方形(不包括正方形). 例如:当 N=2, M= ...
- 怎么用eclipse生成jar文件?eclipse导出jar介绍
1 .我们先要增加jar需要的配置文件,选中项目的src目录,鼠标右键,选择 [New] -选择 [Folder] . 2. 输入META-INF 作为目录名称,点击[Finish] . 3. 选中刚 ...
- linux ABORT的应用详解
NAME ABORT - 退出当前事务 SYNOPSIS ABORT [ WORK | TRANSACTION ] DESCRIPTION 描述 ABORT 回卷当前事务并且废弃所有当前事务中做的更新 ...
- caffe proto
所在目录为: /src/caffe/proto 在caffe.proto中定义了很多结构化数据,比如LayerParameter.Datum.NetParameter.SolverParameter. ...
- 关于MessageBox返回值
风格设置MB_OK. 此时无论点击确定还是点击X,都返回IDOK.风格设置MB_OKCANCEL,点击确认返回IDOK,点击取消和X都返回IDCANCEL.风格设置MB_YESNO,点击是返回IDYE ...
- Eclipse 下载 开源项目 maven依赖丢失和 Deployment Assembly 丢失
周末下载了最新的jeecg的源码来瞅瞅,但是下载后发现,pom文件中定义的依赖都丢失了. 如下图 上网搜索了一下啊,发现需要先给这个项目这个项目 disable maven nature 然后再添加上 ...
- Linux内核网络数据包处理流程
Linux内核网络数据包处理流程 from kernel-4.9: 0. Linux内核网络数据包处理流程 - 网络硬件 网卡工作在物理层和数据链路层,主要由PHY/MAC芯片.Tx/Rx FIFO. ...