HDUOJ 1162 Eddy's picture(最小生成树)
来源:互联网 发布:手机自动签到软件 编辑:程序博客网 时间:2024/05/17 02:06
Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3511 Accepted Submission(s): 1722
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
注意快排对double的实现,由于cmp必须返回int,所以不能return c->len - d->len。
AC code:
#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>using namespace std;const int N=100;typedef struct coodinate{ int v,u;//两个点 double len;//v、u间的距离}Point;Point p[N*(N-1)];int cmp(const void *a,const void *b){ Point *c=(Point *)a; Point *d=(Point *)b; return c->len > d->len ? 1:-1;}int main(){ int i,j,k,t,n,vis[N],m,M; double x[N],y[N],min; while(scanf("%d",&n)!=EOF) { for(i=0;i<n;i++) { scanf("%lf%lf",&x[i],&y[i]); } min=0.0; for(i=0;i<n;i++) vis[i]=i; for(i=0,k=0;i<n;i++) { for(j=i+1;j<n;j++,k++) { p[k].v=i; p[k].u=j; p[k].len=pow((pow((x[i]-x[j]),2.0)+pow((y[i]-y[j]),2.0)),0.5); } } qsort(p,k,sizeof(p[0]),cmp); for(i=0,j=0;j<n-1;i++) { if(vis[p[i].v] != vis[p[i].u]) { min+=p[i].len; j++; //合并集合 if(vis[p[i].v]>vis[p[i].u]) { M=vis[p[i].v]; m=vis[p[i].u]; } else { M=vis[p[i].u]; m=vis[p[i].v]; } for(k=0;k<n;k++) { if(vis[k]==M) vis[k]=m; } } } printf("%.2lf\n",min); } return 0;}
- HDUOJ 1162 Eddy's picture(最小生成树)
- Eddy's picture(最小生成树)
- HDU1162 Eddy's picture 最小生成树
- hdu 1162 Eddy's picture(最小生成树Prim算法)
- hdu 1162 Eddy's picture(最小生成树Kruskal)
- HDOJ 1162:Eddy's picture 求解最小生成树
- HDOJ 1162 Eddy's picture(最小生成树 - kruskal)
- HDU 1162 Eddy's picture 最小生成树
- hdu 1162 Eddy's picture(最小生成树)
- 杭电1162 Eddy's picture 最小生成树
- hdu 1162 Eddy's picture(基础最小生成树)
- HDU 1162 Eddy's picture(最小生成树)
- HDOJ 题目1162 Eddy's picture(最小生成树)
- HDU 1162 Eddy's picture 最小生成树
- HDU 1162 Eddy's picture(图论-最小生成树)
- hdoj 1162 Eddy's picture 【最小生成树】
- hdoj 1162 Eddy's picture(最小生成树)
- hdu 1162 Eddy's picture(最小生成树prim)
- ARM LDR与MOV的区别
- 编程之美1.3 一摞烙饼的排序
- 2012校园招聘求职笔试攻略汇总
- 目前国际上存在的主要安全标准
- linux下安装protobuf教程+示例(详细)
- HDUOJ 1162 Eddy's picture(最小生成树)
- .net socket TCP/udp 程序入门基础
- 《ARM体系结构与编程》学习摘录
- 4月12最近状态
- html下拉表单实现代码
- UBUNTU 添加开机启动程序
- JQuery上传插件Uploadify使用详解
- nfc
- 求一个月的天数