G - EPR悖论

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

SubmitStatus

Description

Many new buildings are under construction on the campus of the University of Waterloo. The university has hired bricklayers, electricians, plumbers, and a computer programmer. A computer programmer? Yes, you have been hired to ensure that each building is connected to every other building (directly or indirectly) through the campus network of communication cables. We will treat each building as a point specified by an x-coordinate and a y-coordinate. Each communication cable connects exactly two buildings, following a straight line between the buildings. Information travels along a cable in both directions. Cables can freely cross each other, but they are only connected together at their endpoints (at buildings). You have been given a campus map which shows the locations of all buildings and existing communication cables. You must not alter the existing cables. Determine where to install new communication cables so that all buildings are connected. Of course, the university wants you to minimize the amount of new cable that you use.

Input

The input file describes several test cases. The description of each test case is given below: The first line of each test case contains the number of buildings N (1 ≤ N ≤ 750). The buildings are labeled from 1 to N. The next N lines give the x and y coordinates of the buildings. These coordinates are integers with absolute values at most 10000. No two buildings occupy the same point. After that there is a line containing the number of existing cables M (0 ≤ M ≤ 1000) followed by M lines describing the existing cables. Each cable is represented by two integers: the building numbers which are directly connected by the cable. There is at most one cable directly connecting each pair of buildings

Output

For each set of input, output in a single line the total length of the new cables that you plan to use rounded to two decimal places.

Sample Input
4
103 104
104 100
104 103
100 100
1
4 2
4
103 104
104 100
104 103
100 100
1
4 2
Sample Output
4.41
4.41

代码:

#include <stdio.h>
#include <string.h>
#include <queue>
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
int pre[800];
int vis[800];
int x[800],y[800];
int n,m,t;
struct node
{
int x,y;
double w;
}map[640000];
int cmp(node a,node b)
{
return a.w<b.w;
}
void init()
{
for(int i=1;i<=n;i++)
pre[i]=i;
}
int find(int x)
{
int r=x;
while(r!=pre[r])
{
r=pre[r];
}
int i,j;
i=x;
while(i!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
int join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
return 1;
}
return 0;
}
void getmap()
{
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x[i],&y[i]);
}
scanf("%d",&m);
int a,b;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
join(a,b);
}
t=0;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
t++;
map[t].x=i;
map[t].y=j;
map[t].w=sqrt((x[i]-x[j])*(x[i]-x[j])*1.0+(y[i]-y[j])*(y[i]-y[j])*1.0);
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
getmap();
sort(map+1,map+t+1,cmp);
double sum=0;
for(int i=1;i<=t;i++)
{
if(join(map[i].x,map[i].y))
{
sum+=map[i].w;
}
}
printf("%.2lf\n",sum);
}
return 0;
}

ACM大一练习赛-第三场------G - EPR悖论克鲁斯卡尔算法相关推荐

  1. ACM大一练习赛-第三场——A - 海森堡不确定原理

    A - 海森堡不确定原理 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  2. HDU 4893 Wow! Such Sequence!(2014年多校联合 第三场 G)(线段树)

    磨了一天的线段树,不能说完全搞清楚,只能说有一个大概的了解,靠着模板才把这道题A了,只能说太弱~~! 题意: 初始时有一字符串,全为0. 三种操作: 1 k d - add  把d加到第k个数上去 2 ...

  3. 牛客网暑期ACM多校训练营(第三场)

    牛客网暑期ACM多校训练营(第三场) A. PACM Team 01背包,输出方案,用bool存每种状态下用的哪一个物品,卡内存.官方题解上,说用char或者short就行了.还有一种做法是把用的物品 ...

  4. 2018牛客网暑假ACM多校训练赛(第三场)I Expected Size of Random Convex Hull 计算几何,凸包,其他...

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-I.html 题目传送门 - 2018牛客多校赛第三场 I ...

  5. 2019.12.31大一练习赛

    2019.12.31大一练习赛 寒假培训第一天,老师认为我们题做得太快了,于是晚上搞了一场练习赛. 第1题 QWQ和QAQ Description QWQ的朋友QAQ开了一个A工厂,但QAQ不是一个很 ...

  6. 求职准备:大一到大三,做好五件大事!

    无论你现在是大几  无论你对于大学是多么的绝望  无论你多么不喜欢你的专业 无论如何 请朋友们好好看完这篇文章  我不能保证你能从此收获多少  但是我保证每一位认真看完的朋友会受益于整个大学生活  他 ...

  7. 大一到大三一定要做完!

    无论你现在是大几 无论你对于大学是多么的绝望 无论你多么不喜欢你的专业 无论如何 请朋友们好好看完这篇文章 我不能保证你能从此收获多少 但是我保证每一位认真看完的朋友会受益于整个大学生活 他会教你少走 ...

  8. 2021年度训练联盟热身训练赛第三场赛后补题

    2021年度训练联盟热身训练赛第三场赛后补题 A Circuit Math [题目分析] [代码展示] B Diagonal Cut [题目分析] [代码展示] C Gerrymandering [题 ...

  9. 2021湖南多校对抗赛第三场

    2021湖南多校对抗赛第三场 排名 第一 第二 第三 中南2队 中南1队 湘潭大学1队 团体成绩 学校 总题数 总罚时 中南大学 26 4396 题解(部分) special thanks: Binb ...

最新文章

  1. php和mysql的概述_PHP的MySQL扩展:MySQL数据库概述_MySQL
  2. 关于迪杰斯特拉算法(最短路)的PHP实现
  3. 9款超绚丽的HTML5/CSS3应用和动画特效
  4. SQL 查询数据库中包含指定字符串的相关表和相关记录
  5. matlab snr eb n0,snr ber Eb/N0之间的区别与联系
  6. 看视频课程的正确方法
  7. 软件测试——网络协议知识(二)
  8. python画二维温度云图_利用python画出词云图
  9. java 模式匹配_java模式匹配之蛮力匹配
  10. 《树莓派渗透测试实战》——1.7 树莓派渗透测试场景
  11. maven内存不足:Unexpected error occurred: Not enough memory to allocate buffers for rehashing Java heap
  12. fastjson safemode_Fastjson远程代码执行漏洞安全通告
  13. 软件著作权算法软件设计说明书_软件著作权设计说明书模板.docx
  14. Esri大赛:添加Arcgis Android sdk 100.1.0
  15. 删除服务器tomcat上项目,删除tomcat服务器
  16. 洛谷 P4704 太极剑
  17. 【Hack The Box】linux练习-- Blunder
  18. 毕业论文管理系统类图
  19. 【GDOI2018模拟7.7】暴力大神hxx 树形dp
  20. apt-get 离线包安装

热门文章

  1. 【Mac】快捷键锁屏
  2. 关于1000BASE-T1 1000BASE-TX和100BASE-T1
  3. 电子设计竞赛学习msp430单片机(msp430g2553,msp430f5529,tmec123G)
  4. PaddleX---Mask RCNN实例分割
  5. 一篇爽文带你全面了解mysql的索引
  6. 迪士尼机器人芭蕾舞_浅析迪士尼跳跳虎机器人
  7. 在CentOS 6.3中安装拼音输入法 详细出处参考:http://www.jb51.net/os/RedHat/73029.html
  8. 设定绘图区坐标轴及标题字体、字号的两种方法
  9. 微信小程序 图片旋转后上传
  10. 外媒:朝鲜导弹发射失败或因美国网络攻击所致