Eddy's picture problem b

本文介绍了一道关于计算几何的问题,即如何通过计算各点间的欧氏距离并使用Prim算法来构建最小生成树,以此来解决连接所有点的最短线段总长度问题。
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?
 

题目大意:

给你一些点的坐标,然后求连通这些点的最小线段的长度。

解题思路:

其实也就是最小生成树的裸题。只不过需要对这n个点全部处理一下,求出每对点之间的长度,然后存入邻接矩阵。然后就KO了。。



  1. #include <stdio.h>  
  2. #include <math.h>  
  3. #include <string.h>  
  4. #define MAXN 105  
  5. #define UPPERDIS 999999  
  6. double lowcost[MAXN],vist[MAXN];  
  7. double cost[MAXN][MAXN];  
  8. int n;  
  9. double prim(int v0)  
  10. {  
  11.     int i, j, minone;  
  12.     double mindis;  
  13.     double ans = 0;/*用来记录最小生成树的总长度*/  
  14.     memset(vist, 0, sizeof(vist));  
  15.     /*各点距离初始化*/  
  16.     for(i = 0;i < n;i++)  
  17.     {  
  18.         lowcost[i] = cost[v0][i];  
  19.     }  
  20.     vist[v0] = 1;  
  21.     for(i = 0;i < n-1;i++)  
  22.     {  
  23.         mindis = UPPERDIS;  
  24.         for(j = 0;j < n;j++)  
  25.           if(!vist[j] && mindis > lowcost[j])  
  26.           {  
  27.               mindis = lowcost[j];  
  28.               minone = j;  
  29.           }  
  30.         /*将找到的最近点加入最小生成树*/  
  31.         ans += mindis;  
  32.         vist[minone] = 1;  
  33.         /*修正其他点到最小生成树的距离*/  
  34.         for(j = 0;j < n;j++)  
  35.           if(!vist[j] && cost[minone][j] < lowcost[j])  
  36.           {  
  37.               lowcost[j] = cost[minone][j];  
  38.           }  
  39.     }  
  40.     return ans;  
  41. }  
  42. struct point {  
  43.     double x, y;  
  44. }p[MAXN];  
  45. double dis(point p1, point p2)  
  46. {  
  47.     return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));  
  48. }  
  49. int main()  
  50. {  
  51.     int i, j;  
  52.     while (~scanf("%d", &n))  
  53.     {  
  54.         for(i = 0; i < n; i++)  
  55.             scanf("%lf%lf", &p[i].x, &p[i].y);  
  56.         for(i = 0; i < n; i++)  
  57.             for(j = i; j < n; j++)  
  58.                 if(j == i)  
  59.                     cost[i][i] = 1000000.0;  
  60.                 else  
  61.                     cost[i][j] = cost[j][i] = dis(p[i], p[j]);  
  62.         printf("%.2lf\n",prim(0));  
  63.     }  
  64. }  
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值