hdu 1162(Kruskal)
来源:互联网 发布:剑灵灵族捏脸数据大全 编辑:程序博客网 时间:2024/05/26 15:59
Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6384 Accepted Submission(s): 3203
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?
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.
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
31.0 1.02.0 2.02.0 4.0
Sample Output
3.41
#include<iostream>#include<cmath>#include<cstdio>#include<algorithm>using namespace std;struct e{int x, y;double w;}edge[5000];double x[105], y[105];int parent[105];int n, m;double length(int i, int j){double a, b;a = x[i]-x[j];b = y[i] - y[j];return sqrt(a*a+b*b);}void initEdge(){m = 0;for(int i = 0; i < n-1; i++){for(int j = i+1; j < n; j++){edge[m].x= i;edge[m].y= j;edge[m++].w = length(i, j);//cout<<edge[m-1].w<<endl;}}}bool comp(e a, e b){return a.w<b.w;}void initSet(){for(int i = 0; i < n; i++){parent[i] = i;}}int find(int x){int r = x;while(r!=parent[r]){r = parent[r];}int temp;while(x!=r){temp = x;x = parent[x];parent[temp] = r;}return r;}void kruskal(){initSet();sort(edge, edge+m, comp);int count = 0;double sum = 0;for(int i = 0; i < m; i++){int x = find(edge[i].x);int y = find(edge[i].y);if(x!=y){count++;//cout<<count<<endl;sum+=edge[i].w;parent[x] = y;if(count==n-1){printf("%.2f\n", sum);break;}}}}int main(){//freopen("in.txt", "r", stdin);while(cin>>n){for(int i = 0; i < n; i++){scanf("%lf %lf", &x[i], &y[i]);}initEdge();kruskal();}return 0;}
0 0
- hdu 1162(Kruskal)
- hdu 1162 最小生成树(Kruskal)
- HDU 1162 Eddy's picture Kruskal算法
- hdu 1102 kruskal
- hdu 1233 kruskal
- hdu 4313(kruskal思想)
- HDU 3371 Kruskal
- HDU 1102 Kruskal
- hdu 1863 kruskal
- HDU 1863-kruskal
- HDU 1875-kruskal
- hdu 1863(prim&kruskal)
- HDU 3367 Pseudoforest(Kruskal)
- HDU 1233 Kruskal算法
- HDU 4463 kruskal 水题
- hdu 1863 Kruskal起步
- hdu 1233 【Kruskal】
- hdu 1162 Eddy's picture(最小生成树Kruskal)
- 如何为Eclipse安装主题(Color Theme)
- java 调用webservice的各种方法总结
- vs2008 高亮 选中
- Freemarker 进行字符串截取
- free命令详解——Linux性能分析
- hdu 1162(Kruskal)
- 使用XStream解序列化时出现问题
- windows上godot 编译
- MYSQL 备份和还原
- 苹果官方网站下载xcode 5.1.1的dmg
- Get the Android SDK
- 20140519
- 【Python】Python对文件的读写操作
- 高性能服务端编程知识点梳理图解