Eddy's picture problem b

来源:互联网 发布:开源mpp数据库 编辑:程序博客网 时间:2024/06/06 05:53
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. }  
0 0
原创粉丝点击