HDU 1162 Eddy's picture 最小生成树
2014-09-24 14:04
483 查看
Eddy's picture |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 164 Accepted Submission(s): 120 |
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 Input3 1.0 1.0 2.0 2.0 2.0 4.0 |
Sample Output3.41 |
/* HDOJ 1162 最小生成树 找出一条连接所有的最短路径 */ #include<iostream> #include<stdio.h> #include<math.h> using namespace std; #define N 101 #define INF 1000000000 struct point{ double x,y; }; point z ; int vis ; int n; double map ,dis ; double getDis(point a,point b) { return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2)); } void Prime() { int i,j,loc; double sum,min; memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) { if(i==1) dis[i]=0; else dis[i]=map[1][i]; } sum=0; for(i=1;i<=n;i++) { min=INF; for(j=1;j<=n;j++) { if(!vis[j]&&dis[j]<min) { min=dis[j]; loc=j; } } vis[loc]=1; sum+=min; for(j=1;j<=n;j++) { if(!vis[j]&&map[loc][j]<dis[j])//loc到j的值 小于dis[j] dis[j]=map[loc][j]; } } printf("%.2lf\n",sum); } int main() { int i,j; //freopen("test.txt","r",stdin); while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) scanf("%lf%lf",&z[i].x,&z[i].y); for(i=1;i<=n;i++) for(j=1;j<=n;j++) map[i][j]=getDis(z[i],z[j]); Prime(); } return 0; }
相关文章推荐
- HDU 1162 Eddy's picture【最小生成树,Prime算法+Kruskal算法】
- HDU1162-Eddy's picture-最小生成树(prim算法模板)
- HDU 1162 Eddy's picture 最小生成树
- hdu 1162 Eddy's picture(基础最小生成树)
- hdoj 1162 Eddy's picture 【最小生成树】
- hdu 1162 Eddy's picture(最小生成树之kruskal算法)
- hdoj 1162 Eddy's picture【最小生成树 kruskal && prim】
- HDU 1162 Eddy's picture(最小生成树)
- HDU 1162 Eddy's picture(最小生成树-Prim)
- hdoj 1162 Eddy's picture【最小生成树】
- hdu 1162 Eddy's picture (Kruskal算法,prim算法,最小生成树)
- hdu 1162 Eddy's picture(最小生成树)
- hdu 1162 Eddy's picture(最小生成树prim)
- hdu 1162 Eddy's picture (Kruskal算法,prim算法,最小生成树)
- HDU 1162 Eddy's picture (最小生成树)
- HDOJ 1162 Eddy's Picture (最小生成树)
- 【杭电oj】1162 - Eddy's picture(最小生成树)
- HDU 1162 Eddy's picture(点连通:最小生成树)
- 杭电ACM1162——Eddy's picture~~最小生成树
- 最小生成树(模板题)Eddy's picture HDU - 1162