Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10803   Accepted: 3062

Description

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 (XiYi) on the plane (0 ≤ X≤ 1,000,000; 0 ≤ Y≤ 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.

Input

* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Two space-separated integers: Xand Y
* 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.

Output

* 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.

Sample Input

4 1
1 1
3 1
2 3
4 3
1 4

Sample Output

4.00

Source

 
kruskal最小生成树
先处理出每两个点之间的距离,加入到边集。读入的已连接的点对也加入边集,距离为0。跑一遍kruskal出解
 
 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
int n,m;
struct point{
double x,y;
}p[mxn];
struct edge{
int x,y;
double dis;
}e[mxn*mxn];
int cmp(edge a,edge b){
return a.dis<b.dis;
}
double dist(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
//
int fa[mxn];
int find(int x){
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
int cnt=;
double ans=;
void kruskal(){
int now=;
for(int i=;i<=cnt && now<n-;i++){
int x=find(e[i].x);
int y=find(e[i].y);
if(x!=y){
ans+=e[i].dis;
fa[x]=y;now++;
}
}
printf("%.2f\n",ans);
return;
}
int main(){
scanf("%d%d",&n,&m);
int i,j;
for(i=;i<=n;i++) fa[i]=i;
for(i=;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
for(i=;i<n;i++)
for(j=i+;j<=n;j++){
e[++cnt].dis=dist(p[i],p[j]);
e[cnt].x=i;e[cnt].y=j;
}
for(i=;i<=m;i++){
cnt++;
scanf("%d%d",&e[cnt].x,&e[cnt].y);
e[cnt].dis=;
}
sort(e+,e+cnt+,cmp);
kruskal();
return ;
}

POJ3625 Building Roads的更多相关文章

  1. poj 3625 Building Roads

    题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several ...

  2. poj 2749 Building roads (二分+拆点+2-sat)

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6229   Accepted: 2093 De ...

  3. BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )

    计算距离时平方爆了int结果就WA了一次...... ------------------------------------------------------------------------- ...

  4. HDU 1815, POJ 2749 Building roads(2-sat)

    HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...

  5. Building roads

    Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  6. bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads kruskal求最小生成树. #include<iostream> #include<cstdio> ...

  7. [POJ2749]Building roads(2-SAT)

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8153   Accepted: 2772 De ...

  8. bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树

    1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer J ...

  9. 洛谷——P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

随机推荐

  1. 深入理解ES6箭头函数的this以及各类this面试题总结

    ES6中新增了箭头函数这种语法,箭头函数以其简洁性和方便获取this的特性,俘获了大批粉丝儿 它也可能是面试中的宠儿, 我们关键要搞清楚 箭头函数和普通函数中的this 一针见血式总结: 普通函数中的 ...

  2. .pyc文件的结构体PyCodeObject

    python执行程序时生成的pyc文件里面是,PyCodeObject 的结构体构成,每个命名空间(函数名.import模块等)都会形成一个core block,一个python程序的所有命名空间生成 ...

  3. php通过geohash算法实现查找附近的商铺

    geohash有以下几个特点: 首先,geohash用一个字符串表示经度和纬度两个坐标.利用geohash,只需在一列上应用索引即可. 其次,geohash表示的并不是一个点,而是一个矩形区域.比如编 ...

  4. PHP分页类代码

    今天为大家献上一段代码,这是一段php分页代码: <?php//创建分页类class Page {    private $_all_num;                           ...

  5. LeetCode(217)Contains Duplicate

    题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...

  6. 10个MCU常用的基础知识

    转自:http://bbs.21ic.com/icview-2659278-1-1.html 1.MCU有串口外设的话,在加上电平转换芯片,如MAX232.SP3485就是RS232和RS485接口了 ...

  7. hdu 5533

    Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  8. 加密javascript代码

    最近看了个js日历,里面用到了加密,看了下,自己也模仿做加密,现在只能加密一般的javascript语句 <!DOCTYPE html> <html> <meta htt ...

  9. Jquery+Ajax+asp.net+sqlserver-编写的通用邮件管理(有源码)

    开始 邮件管理通常用在各个内部系统中,为了方便快捷的使用现有的代码开发一个邮件管理系统而诞生的. 准备条件 这是我的设计表结构,大家一看就懂了 --邮件接收表CREATE TABLE [dbo].[T ...

  10. Java项目引入eclipse注意事项

    我以前也搞过java,后转前端,接触html+css+js时间比较多,所以java后端也忘了差不多.最近负责公司的邮件系统项目,项目是java语言写,项目架构比较复杂,在部署项目的时候,遇到了很多问题 ...