BZOJ——1626: [Usaco2007 Dec]Building Roads 修建道路
http://www.lydsy.com/JudgeOnline/problem.php?id=1626
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 1787 Solved: 755
[Submit][Status][Discuss]
Description
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)条路分别连接了哪两个农场,他希望你计算一下,为了使得所有农场连通,他所需建造道路的最小总长是多少。
Input
* 第1行: 2个用空格隔开的整数:N 和 M
* 第2..N+1行: 第i+1行为2个用空格隔开的整数:X_i、Y_i * 第N+2..N+M+2行: 每行用2个以空格隔开的整数i、j描述了一条已有的道路, 这条道路连接了农场i和农场j
Output
* 第1行: 输出使所有农场连通所需建设道路的最小总长,保留2位小数,不必做 任何额外的取整操作。为了避免精度误差,计算农场间距离及答案时 请使用64位实型变量
Sample Input
1 1
3 1
2 3
4 3
1 4
输入说明:
FJ一共有4个坐标分别为(1,1),(3,1),(2,3),(4,3)的农场。农场1和农场
4之间原本就有道路相连。
Sample Output
输出说明:
FJ选择在农场1和农场2间建一条长度为2.00的道路,在农场3和农场4间建一
条长度为2.00的道路。这样,所建道路的总长为4.00,并且这是所有方案中道路
总长最小的一种。
HINT
Source
#include <algorithm>
#include <cstdio>
#include <cmath> inline void read(int &x)
{
x=; register char ch=getchar();
for(; ch>''||ch<''; ) ch=getchar();
for(; ch>=''&&ch<=''; ch=getchar()) x=x*+ch-'';
}
const int N();
double ans;
int n,m,t,cnt;
int fa[N],x[N],y[N];
struct Road {
int x,y;double dis;
bool operator < (const Road&x)const
{
return dis<x.dis;
}
}road[N*N>>]; inline double Get_Dis(int i,int j)
{
double t1=(.*x[i]-.*x[j])*(.*x[i]-.*x[j]);
double t2=(.*y[i]-.*y[j])*(.*y[i]-.*y[j]);
return sqrt(.*t1+.*t2);
} int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); } int Presist()
{
read(n),read(m);
for(int i=; i<=n; ++i)
read(x[i]),read(y[i]),fa[i]=i;
for(int u,v,i=; i<=m; ++i)
read(u),read(v),fa[find(v)]=find(u);
for(int i=; i<=n; ++i)
for(int j=i+; j<=n; ++j)
{
road[++cnt].dis=Get_Dis(i,j);
road[cnt].x=i,road[cnt].y=j;
}
std::sort(road+,road+cnt+);
for(int fx,fy,i=; i<=cnt; ++i)
{
fx=find(road[i].x),
fy=find(road[i].y);
if(fx==fy) continue;
ans+=road[i].dis; fa[fx]=fy;
if(++t==n-) break;
}
printf("%.2lf",ans);
return ;
} int Aptal=Presist();
int main(int argc,char**argv){;}
BZOJ——1626: [Usaco2007 Dec]Building Roads 修建道路的更多相关文章
- BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )
计算距离时平方爆了int结果就WA了一次...... ------------------------------------------------------------------------- ...
- bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树
1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer J ...
- BZOJ 1626 [Usaco2007 Dec]Building Roads 修建道路:kruskal(最小生成树)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1626 题意: 有n个农场,坐标为(x[i],y[i]). 有m条原先就修好的路,连接农场( ...
- bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路【最小生成树】
先把已有的边并查集了,然后MST即可 记得开double #include<iostream> #include<cstdio> #include<algorithm&g ...
- 【BZOJ】1626: [Usaco2007 Dec]Building Roads 修建道路(kruskal)
http://www.lydsy.com/JudgeOnline/problem.php?id=1626 依旧是水题..太水了.. #include <cstdio> #include & ...
- [Usaco2007 Dec]Building Roads 修建道路[最小生成树]
Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农 ...
- bzoj1626[Usaco2007 Dec]Building Roads 修建道路
Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农 ...
- [Usaco2007 Dec]Building Roads 修建道路
题目描述 Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场).有些农场 ...
- BZOJ 1692: [Usaco2007 Dec]队列变换( 贪心 )
数据 n <= 30000 , 然后 O( n² ) 的贪心也过了..... USACO 数据是有多弱啊 = = ( ps : BZOJ 1640 和此题一模一样 , 双倍经验 ) ------ ...
随机推荐
- Bootstrap历练实例:小的按钮
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- shell脚本,文件里面的英文大小写替换方法。
[root@localhost wyb]# cat daxiaoxie qweBNMacb eeeDFSmkl svdIOPtyu [root@localhost wyb]# cat daxiaoxi ...
- OpenCV Haar AdaBoost源码改进据说是比EMCV快6倍
<pre name="code" class="cpp">#include "Haar.h" #include "lo ...
- 选择法数组排序参考(Java)
package com.swift; public class Xuanze { public static void main(String[] args) { int[] arr= {28,2,3 ...
- iOS开发遇到的坑之一: 开发遇见如下错误:Undefined symbols for architecture arm64
博客处女作,写得不好望谅解! “for architecture arm64”就是说没有支持arm64,在Build settings里architecture相关的几项需要配置正确 在最近升级coc ...
- 企业版https
http://www.cocoachina.com/bbs/read.php?tid=194213
- C++高精度乘法
#include <cstdio> #include <iostream> #include <algorithm> void highPrecision (int ...
- Day17re模块和hashlib模块
re模块 正则表达式 用一些特殊符号拼凑成的规则,去字符串中匹配到符合规则的东西 为什么有正则表达式 从字符串中取出想要的数据 怎么用正则表达式 re.findall()结果存成列表 \w 匹配一个字 ...
- sql自动审核工具-inception
[inception使用规范及说明文档](http://mysql-inception.github.io/inception-document/usage/)[代码仓库](https://githu ...
- 【bzoj1109】[POI2007]堆积木Klo 动态规划+树状数组
题目描述 Mary在她的生日礼物中有一些积木.那些积木都是相同大小的立方体.每个积木上面都有一个数.Mary用他的所有积木垒了一个高塔.妈妈告诉Mary游戏的目的是建一个塔,使得最多的积木在正确的位置 ...