hdu1162 Eddy's picture 基础最小生成树

 1     #include <cstdio>
 2     #include <cmath>
 3     #include <cstring>
 4     #include <algorithm>
 5     #include <string>
 6     using namespace std;
 7
 8     int n, m;
 9     int f[510];
10
11     struct node
12     {
13         int u, v;
14         double w;
15     }map[10010];
16
17     int getf(int a)
18     {
19         if (f[a] == a)
20         {
21             return a;
22         }
23
24         f[a] = getf(f[a]);
25         return f[a];
26     }
27
28     void merge(int a, int b)
29     {
30         int x = getf(a);
31         int y = getf(b);
32
33         if (x != y)
34             f[y] = x;
35     }
36
37
38     void init()
39     {
40         for (int i = 1; i <= n; i++)
41             f[i] = i;
42     }
43
44     bool cmp(node a, node b)
45     {
46         return a.w < b.w;
47     }
48
49     double kura()
50     {
51         double res = 0;
52         sort(map, map + m, cmp);
53
54         for (int i = 0; i < m; i++)
55         {
56             if (getf(map[i].u) != getf(map[i].v))
57             {
58                 merge(map[i].u, map[i].v);
59                 res += map[i].w;
60             }
61         }
62         return res;
63     }
64
65     double a[102][2];
66
67
68     int main()
69     {
70         while (scanf("%d", &n) != EOF)
71         {
72             m = n*(n - 1);
73             for (int i = 1; i <= n; i++)
74                 scanf("%lf %lf", &a[i][0], &a[i][1]);
75
76             int k = 0;
77             for (int i = 1; i <= n; i++)
78             {
79                 for (int j = i + 1; j <= n; j++)
80                 {
81                     map[k].u = i;
82                     map[k].v = j;
83                     map[k].w = sqrt(abs(pow(a[j][0] - a[i][0], 2) + pow(a[j][1] - a[i][1], 2)));
84                     k++;
85                 }
86             }
87             init();
88             printf("%.2lf\n", kura());
89         }
90
91         return 0;
92     }

hdu1162 Eddy's picture 基础最小生成树

时间: 2024-10-10 17:49:40

hdu1162 Eddy's picture 基础最小生成树的相关文章

HDU1162 Eddy&#39;s picture 【最小生成树Prim】

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6723    Accepted Submission(s): 3391 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

Eddy&#39;s picture(最小生成树)

Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 228 Accepted Submission(s): 168   Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become

HDU 1162 Eddy&#39;s picture【最小生成树,Prime算法+Kruskal算法】

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9334    Accepted Submission(s): 4711 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

hdu 1162 Eddy&#39;s picture(最小生成树)

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 i

hdu Eddy&#39;s picture (最小生成树)

Eddy's picture Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 29   Accepted Submission(s) : 26 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Eddy begins to like pa

hdoj 1162 Eddy&#39;s picture【最小生成树】

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8117    Accepted Submission(s): 4114 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

HDU1162 Eddy&#39;s picture【Prim】

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7231    Accepted Submission(s): 3656 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

HDU 1162 Eddy&#39;s picture (最小生成树 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 res

【HDU1162】Eddy&#39;s picture(MST基础题)

很基础的点坐标MST,一不留神就AC了, - - !! 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include <numeric> 9 #include &