Eddy's picture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8797    Accepted Submission(s): 4476

Problem Description
Eddy
begins to like painting pictures recently ,he is sure of himself to
become a painter.Every day Eddy draws pictures in his small room, and
he usually puts out his newest pictures to let his friends appreciate.
but the result it can be imagined, the friends are not interested in his
picture.Eddy feels very puzzled,in order to change all friends 's view
to his technical of painting pictures ,so Eddy creates a problem for
the his friends of you.
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
The
first line contains 0 < n <= 100, the number of point. For each
point, a line follows; each following line contains two real numbers
indicating the (x,y) coordinates of the point.

Input contains multiple test cases. Process to the end of file.

 
Output
Your
program prints a single real number to two decimal places: the minimum
total length of ink lines that can connect all the points.
 
Sample Input
3
1.0 1.0
2.0 2.0
2.0 4.0
 
Sample Output
3.41
题意:大意就是给你n个点求出这n个点形成的最小生成树。
题解:kruskal算法解。。开始的时候将边初始化成了0,WA了两次..好久没打过生疏了。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std; const int N = ;
struct Point {
double x,y;
}p[N];
struct Edge{
int s,e;
double len;
}edge[N*(N-)/];
int father[N],n;
double dis(Point a,Point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int _find(int x){
if(x==father[x]) return x;
return _find(father[x]);
}
int cmp(Edge a,Edge b){
return a.len<b.len;
}
double kruskal(int m){
sort(edge+,edge+m+,cmp);
double cost=;
for(int i=;i<=m;i++){
int x = _find(edge[i].s);
int y = _find(edge[i].e);
if(x!=y){
father[x] = y;
cost +=edge[i].len;
}
}
return cost;
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=;i<n;i++) father[i] = i;
for(int i=;i<n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
}
int m =; ///边的数量
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){ ///这里是i+1开始,从0开始会多出很多边
edge[m].s =i;
edge[m].e = j;
edge[m++].len = dis(p[i],p[j]);
}
}
m--; ///记得
printf("%.2lf\n",kruskal(m));
}
}

最新文章

  1. Mysql5.5升级到5.7后MySQLdb不能正常使用的问题解决
  2. 【BZOJ-2555】SubString 后缀自动机 + LinkCutTree
  3. WebService入门
  4. Codeforces Round #388 (Div. 2) - B
  5. 1125Sending data
  6. 周爱民:真正的架构师是没有title的(图灵访谈)
  7. Linux的学习--使用PuTTY
  8. Android Studio简单设置
  9. zabbix报警媒介------&gt;微信报警
  10. DailyReport自动保存工具
  11. Git 远程分支的查看及相关问题
  12. N!末尾有多少个零
  13. Java 基础(一)
  14. 超级易使用的jquery视频背景插件Vide
  15. Python 学习笔记6
  16. Explicit Semantic Analysis (ESA)
  17. Sublime3中如何安装markdown插件支持
  18. nowcoder16450 托米的简单表示法
  19. js 解析url
  20. FTP 150 Opening BINARY mode data connection for MLSD 读取目录列表失败

热门文章

  1. AD9 设置网络标号作用域
  2. 2139: road
  3. 【Invert Binary Tree】cpp
  4. Jmeter编码问题
  5. Rbac_权限管理
  6. CentOS 6.3安装配置LAMP服务器(Linux+Apache+MySQL+PHP5)
  7. BI商业智能培训系列——(二)SSIS入门
  8. BZOJ3631 [JLOI2014]松鼠的新家 【树上差分】
  9. BZOJ2157 旅游 【树剖 或 LCT】
  10. 洛谷 P1503 鬼子进村 解题报告