Hdu 1162 Eddy's picture【最小生成树】
2015-08-14 00:48
543 查看
Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8141 Accepted Submission(s): 4130
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
[align=left]Sample Input[/align]
3
1.0 1.0
2.0 2.0
2.0 4.0
[align=left]Sample Output[/align]
3.41
一道简单的最小生成树题目,只需要求出最小生成树的总权值就可以了,水过.....
注意坐标和长度的转换...
#include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; int per[205],n; double a[205],b[205]; struct lu { int a,b; double len; }x[250005]; double dis(int i,int j) { return sqrt((a[i]-a[j])*(a[i]-a[j])*1.0+(b[i]-b[j])*(b[i]-b[j])); } void init() { for(int i=1;i<=n;++i) { per[i]=i; } } bool cmp(lu a,lu b) { return a.len<b.len; } int find(int x) { int r=x; while(r!=per[r]) { r=per[r]; } int i=x,j; while(i!=r) { j=per[i];per[i]=r;i=j; } return r; } int join(int x,int y) { int fx=find(x),fy=find(y); if(fx!=fy) { per[fy]=fx; return 1; } return 0; } void kruskal() { int cnt=0;double maxn=0; for(int i=0;cnt<n-1;++i) { if(join(x[i].a,x[i].b)) { maxn+=x[i].len; ++cnt; } } printf("%.2lf\n",maxn); } int main() { int i,j; while(~scanf("%d",&n)) { init(); for(i=0;i<n;++i) { scanf("%lf%lf",a+i,b+i); } int c=0; for(i=0;i<n-1;++i) { for(j=i+1;j<n;++j) { x[c].a=i+1;x[c].b=j+1; x[c].len=dis(i,j); ++c; } } sort(x,x+c,cmp); kruskal(); } return 0; }
相关文章推荐
- 多维数组的定义和存储
- QList类介绍
- hdoj 2242 考研路茫茫——空调教室 【无向图求边双联通 缩点 + 树形dp】
- UVA 10369 Arctic Network【最小生成树】
- leetcode#195-tenthline
- Poj 2253 Frogger【最小生成树(巧解)】
- 【Android 分享】ShareSDK微信分享详解
- Eplan PPE Pro-panel Electric fluid P8 2.4图文安装教程
- NAT(Network Address Translation)
- JQuery
- Swap Nodes in Pairs
- 第一篇文章
- 黑马程序员—-C语言入门十重奏之六相濡以沫
- python 导入自定义包 包机制
- hdu 4288(区间合并)
- LeetCode Excel Sheet Column Number
- Android drawable selector 设置无效
- LeetCode Excel Sheet Column Number
- Java学习-024-获取当前类名或方法名二三文
- Oracle_绑定变量窥探测试