hdu1162 Eddy's picture(prim)

Eddy's picture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3651    Accepted Submission(s): 1793


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 Input
3
1.0 1.0
2.0 2.0
2.0 4.0
  
Sample Output
3.41
 
分析:经典prim,把每个坐标看成一个顶点,依次编号1至n,于是很方便的可以使用邻接矩阵了。
心得:我老是在循环(n-1)次找顶点时,总是喜欢循环 n 次,下次不要了!!!
View Code
 1 #include<iostream>
 2 #include<cmath>
 3 #include<cstdio>
 4 #define N 110
 5 using namespace std;
 6 
 7 struct coord{double x,y;}point[N];
 8 double map[N][N],lowcost[N];
 9 bool s[N];
10 
11 double prim(int n)
12 {
13     int i,j,k;
14     double min,ans=0;
15     s[1]=true;
16     for(i=2;i<=n;i++)
17     {
18         lowcost[i]=map[1][i];
19         s[i]=false;
20     }
21     for(i=1;i<n;i++)//只要需要n-1条边,所以只能循环n-1次
22     {
23         min=2000000000;
24         for(j=2;j<=n;j++)
25         {
26             if(min>lowcost[j]&&!s[j])
27             {
28                 min=lowcost[j];
29                 k=j;
30             }
31         }
32         ans+=min;s[k]=true;
33         for(j=2;j<=n;j++)
34         {
35             if(lowcost[j]>map[k][j]&&!s[j])
36                 lowcost[j]=map[k][j];
37         }
38     }
39     return ans;
40 }
41 
42 int main()
43 {
44     int n,i,j;
45     double t1,t2;
46     while(cin>>n)
47     {
48         for(i=1;i<=n;i++)
49         {
50             cin>>point[i].x>>point[i].y;
51         }
52         for(i=1;i<=n;i++)
53         {
54             for(j=i+1;j<=n;j++)
55             {
56                 t1=fabs(point[i].x-point[j].x);
57                 t2=fabs(point[i].y-point[j].y);
58                 map[j][i]=map[i][j]=sqrt(t1*t1+t2*t2);
59             }
60         }
61         printf("%.2lf\n",prim(n));
62     }
63     return 0;
64 }

 

转载于:https://www.cnblogs.com/zhourongqing/archive/2012/05/05/2484508.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值