HDUOJ-----(1162)Eddy's picture(最小生成树)

Eddy's picture

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


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
 

 

Author
eddy
 
此题数据规模娇小,prim随意处理即合..........
代码:
 1 #include<cstdio>

 2 #include<cstring>

 3 #include<cmath>

 4 #include<algorithm>

 5 using namespace std;

 6 const int inf=0x3f3f3f3f;

 7 const int maxn=105;

 8 double map[maxn][maxn];

 9 double lowc[maxn];

10 bool vis[maxn];

11 struct point{

12 

13   double x,y;

14   int id;

15 };

16 

17 inline double dista(point a,point b){

18   return sqrt((a.y-b.y)*(a.y-b.y)+(a.x-b.x)*(a.x-b.x));

19 }

20 

21 point sac[maxn];

22 

23 double prim(int st,int n)

24 {

25     memset(vis,0,sizeof(vis));

26     vis[st]=true;

27     double minc=inf;

28     for(int i=1;i<=n;i++)

29         lowc[i]=map[st][i];

30     int pre=st;

31     double sum=0.0;

32     for(int i=1;i<n;i++){

33         minc=inf;

34         for(int j=1;j<=n;j++)

35         {

36             if(!vis[j]&&minc>lowc[j]){

37                 minc=lowc[j];

38                 pre=j;

39             }

40         }

41          sum+=minc;

42          vis[pre]=true;

43         for(int j=1;j<=n;j++){

44             if(!vis[j]&&lowc[j]>map[pre][j]){

45                 lowc[j]=map[pre][j];

46             }

47         }

48     }

49     return sum;

50 }

51 void work(int n)

52 {

53     for(int i=0;i<n;i++){

54         for(int j=0;j<n;j++){

55           map[i+1][j+1]=map[j+1][i+1]=dista(sac[i],sac[j]);

56         }

57     }

58 }

59 int  main(){

60    int n;

61    while(scanf("%d",&n)!=EOF){

62 

63       for(int i=0;i<n;i++)

64       {

65         scanf("%lf%lf",&sac[i].x,&sac[i].y);

66         sac[i].id=i+1;

67       }

68      work(n);

69      printf("%.2lf\n",prim(1,n));

70    }

71    return 0;

72 }
View Code

 

 

你可能感兴趣的:(最小生成树)