杭电--1162--Eddy's picture--并查集
来源:互联网 发布:映客 h5 源码 编辑:程序博客网 时间:2024/06/06 10:04
Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5178 Accepted Submission(s): 2566
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
Input contains multiple test cases. Process to the end of file.
31.0 1.02.0 2.02.0 4.0
3.41
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int father[111111];
double s;
struct ssss
{
double a,b;
}ss[111];
struct dddd
{
int a,b;
double x;
}dd[5000];
int Find(int a)
{
return a==father[a]?a:father[a]=Find(father[a]);
}
void Union(int i)
{
int a=Find(dd[i].a),b=Find(dd[i].b);
if(a!=b)father[a]=b,s+=dd[i].x; //只有没有连通的才能进行这一步,所以每次都是需要连通的最短距离
}
bool cmp(const dddd &a,const dddd &b)
{
return a.x<b.x;
}
int main (void)
{
int n,i,j,k,l;
while(cin>>n)
{
for(i=0;i<n;i++)
cin>>ss[i].a>>ss[i].b;
for(i=0;i<111111;i++)father[i]=i; //初始化
for(i=l=0;i<n;i++)
for(j=i+1;j<n;j++)
{
dd[l].a=i,dd[l].b=j;
double x=ss[i].a-ss[j].a,y=ss[i].b-ss[j].b;
dd[l++].x=sqrt(x*x+y*y); //老规矩,把距离存两点一起
}
sort(dd,dd+l,cmp);
for(i=0,s=0;i<l;i++)
Union(i); //并起来
printf("%.2f\n",s);
}
return 0;
}
- 杭电--1162--Eddy's picture--并查集
- 杭电1162 Eddy's picture
- 杭电Hdu 1162 Eddy's picture
- Eddy's picture 杭电oj 1162
- 杭电1162Eddy's picture
- 【杭电】[1162]Eddy's picture
- HDOJ Eddy's picture(kruskal+并查集)
- Eddy's picture(并查集+最小生成树)
- hdu1162 Eddy's picture 并查集+最小生成树
- hdu 1162 Eddy's picture (并查集+快排)
- 杭电1162 Eddy's picture 最小生成树
- 杭电1162Eddy's picture(最小生成树)
- 杭电1162Eddy's picture(prim算法)
- 1162 Eddy's picture
- Eddy's picture 1162
- hdu1162(并查集+Kruskal最小生成树)-- Eddy's picture
- 杭电 ACM HDU 1162 Eddy's picture (最短路 prim算法)
- 【杭电oj】1162 - Eddy's picture(最小生成树)
- 一个老程序员的心理话
- 成功的网站靠的是推广而不是技术
- JAVA 正则表达式 (超详细)
- csapp2e 家庭作业 6.24
- 关于软件人员跳槽
- 杭电--1162--Eddy's picture--并查集
- sed使用心得
- Hadoop集群配置(最全面总结)
- css 水平居中的方法
- poj-3899-The Lucky Numbers 模拟+数学
- 毫无保留开源我写的:IOS Android Ipad 多点触摸通用js 库
- 使用Dotfuscator混淆winphone8应用XAP
- 杭电--1272--小希的迷宫--并查集
- csapp2e 家庭作业 6.25