洛谷 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.
Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场)。有些农场之间原本就有道路相连。 所有N(1 <= N <= 1,000)个农场(用1..N顺次编号)在地图上都表示为坐标为(X_i, Y_i)的点(0 <= X_i <= 1,000,000;0 <= Y_i <= 1,000,000),两个农场间道路的长度自然就是代表它们的点之间的距离。现在Farmer John也告诉了你农场间原有的M(1 <= M <= 1,000)条路分别连接了哪两个农场,他希望你计算一下,为了使得所有农场连通,他所需建造道路的最小总长是多少。
输入格式
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.
第1行: 2个用空格隔开的整数:N 和 M
第2..N+1行: 第i+1行为2个用空格隔开的整数:X_i、Y_i
第N+2..N+M+2行: 每行用2个以空格隔开的整数i、j描述了一条已有的道路, 这条道路连接了农场i和农场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.
输出使所有农场连通所需建设道路的最小总长,保留2位小数,不必做 任何额外的取整操作。为了避免精度误差,计算农场间距离及答案时 请使用64位实型变量
输入输出样例
输入 #1
4 1
1 1
3 1
2 3
4 3
1 4
输出 #1
4.00
说明/提示
题目简述:给出n个点的坐标,其中一些点已经连通,现在要把所有点连通,求修路的最小长度.
感谢@睿屿青衫丶 提供翻译
【思路】
最小生成树 + 并查集
在转换为double类型上面花了好久的时间
一只忽略了这个小东西
先读入数据
将已经连接起来的点都合并起来
如果这些就够用了
那就输出0
不然先预处理出所有的
没有被连接起来的边
一共就1000*1000个
还可以稍加优化比如在没举的时候(详见代码)
然后就成了1—1000累加起来那么就是跟少了哦
将两个点和他俩算出来的距离用一个结构体存起来
排一个序
然后从小到大枚举跑克鲁斯卡尔就好了
注意:
一定要在求距离的时候将int转换为double
可以用乘以一个1.0来解决
【完整代码】
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int Max = 1005;
int x[Max],y[Max];
struct node
{
int i,j;
double v;
}a[Max * Max];
int father[Max * Max];
double work(int i,int j)
{
double aa = (x[i] - x[j] * 1.0) * (x[i] - x[j] * 1.0);
double bb = (y[i] - y[j] * 1.0) * (y[i] - y[j] * 1.0);//强制类型转换
return sqrt(aa + bb);
}
bool cmp(const node x,const node y)
{
return x.v < y.v;
}
int find(int x)
{
if(father[x] != x)father[x] = find(father[x]);
return father[x];
}
void hebing(int x,int y)
{
x = find(x);
y = find(y);
father[x] = y;
}
int main()
{
int n,m;
cin >> n >> m;
for(register int i = 1;i <= n;++ i)
father[i] = i;
for(register int i = 1;i <= n;++ i)
cin >> x[i] >> y[i];
int qwq,awa;
int js = 0;
for(register int i = 1;i <= m;++ i)
{
cin >> qwq >> awa;
if(find(qwq) != find(awa))
hebing(qwq,awa),js ++;
if(js == n - 1)break;
}
int jj = 0;
for(register int i = 1;i <= n;++ i)
for(register int j = i + 1;j <= n;++ j)//避免重复保证出现了1,2之后不会再出现2,1,是不是很简单?
a[++ jj].i = i,a[jj].j = j,a[jj].v = work(i,j);
sort(a + 1,a + 1 + jj,cmp);
double ans = 0;
for(register int i = 1;i <= jj;++ i)
{
if(find(a[i].i) != find(a[i].j))
hebing(a[i].j,a[i].i),js ++,ans += a[i].v;
if(js == n - 1)break;
}
printf("%.2lf\n",ans);
return 0;
}
洛谷 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 ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解
题面 这道题是一道标准的01分数规划: 但是有一些细节可以优化: 不难想到要二分一个mid然后判定图上是否存在一个环S,该环是否满足∑i=1t(Fun[vi]−mid∗Tim[ei])>0 但是 ...
随机推荐
- [高清] Spring揭秘完整高清版
------ 郑重声明 --------- 资源来自网络,纯粹共享交流, 如果喜欢,请您务必支持正版!! --------------------------------------------- ...
- gitlab用户登录与AD域用户集成
---恢复内容开始--- 编辑gitlab.rb文件 sudo vi /etc/gitlab/gitlab.rb 下图是我编辑的内容示例(仅供参考): 编辑以下内容: gitlab_rails['ld ...
- BUAA OO 2019 第四单元作业总结
目录 第四单元总结 总 UML UML 类图 UML 时序图 UML 状态图 架构设计 第十三次作业 第十四次作业 课程总结 历次作业总结 架构设计 面向对象方法理解 测试方法理解与实践 改进建议 尽 ...
- 【转载】C#中string类使用Replace方法来替换字符串
在C#的字符串操作过程中,有时候需要替换字符串中的某个子字符串,此时就可以使用到字符串类自带的Replace方法来实现,Replace方法将查找到所有符合被替换的子字符串,然后将之全部替换为目标字符串 ...
- H5打开app指定页面(H5+app项目)
H5+app项目,在HBuilderX中设置 详情参考官方 https://ask.dcloud.net.cn/article/64 给h5+app设置scheme值,作用:在其它app和h5页面中启 ...
- Awesome Mac OS Command Line 中文翻译
awesome-macos-command-line 收集了很多有趣的 Mac 终端命令. 看了一遍后,发现帮助很大. 见识许多没有使用过的命令,加深了对 Mac 的认识. 所以翻译成了中文,共享给其 ...
- ABAP-Eclipse ADT中创建ABAP CDS视图
Create an ABAP Project in ABAP Development Tools (ADT): https://developers.sap.com/tutorials/abap-cr ...
- nginx 是如何分配 worker 进程连接数的
客户端连接过来后,多个空闲的进程,会竞争这个连接,很容易看到,这种竞争会导致不公平,如果某个进程得到 accept 的机会比较多,它的空闲连接很快就用完了,如果不提前做一些控制,当 accept 到一 ...
- linux下安装grpc插件 (c++和go语言)
在debian/ubuntu系统下,需要做如下准备操作: $ [sudo] apt-get install build-essential autoconf libtool pkg-config 如果 ...
- 模块之 time datetime random json pickle os sys hashlib collections
目录 1. time模块 1.1表示时间的几种方式: 1.2格式化字符串的时间格式 1.3不同格式时间的转换 2.datetim模块 3.random模块 4. json模块 4.1dumps.loa ...