【HDU1162】Eddy'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 <limits.h>
10
11 #define typec double
12 using namespace std;
13
14 const int inf = 0xffff;
15 const int V = 105;
16 int vis[V];
17 typec lowc[V], Map[V][V], point[V][2];
18
19 typec prim (typec cost[][V], int n) {
20     int i, j, p;
21     typec minc, res = 0;
22     memset(vis, 0, sizeof(vis));
23     vis[0] = 1;
24     for (i = 1; i < n; ++ i) lowc[i] = cost[0][i];
25     for (i = 1; i < n; ++ i) {
26         minc = inf;
27         p = -1;
28         for (j = 0 ; j < n; ++ j) {
29             if (0 == vis[j] && minc > lowc[j]) {
30                 minc = lowc[j];
31                 p = j;
32             }
33         }
34         if (inf == minc) return -1;
35         res += minc;
36         vis[p] = 1;
37         for (j = 0 ; j < n; ++ j) {
38             if (0 == vis[j] && lowc[j] > cost[p][j]) {
39                 lowc[j] = cost[p][j];
40             }
41         }
42     }
43     return res;
44 }
45
46 double cal_dis (double x1, double y1, double x2, double y2) {
47     return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
48 }
49
50 int main () {
51     int n;
52     while (~scanf("%d", &n)) {
53
54         for (int i = 0 ; i < n; ++ i) {
55             scanf("%lf %lf", &point[i][0], &point[i][1]);
56         }
57
58         for (int i = 0 ; i < V; ++ i) {
59             for (int j = 0; j < V; ++ j) {
60                 if (i == j) Map[i][j] = 0;
61                 else Map[i][j] = inf;
62             }
63         }
64
65         for (int i = 0; i < n; ++ i) {
66             for (int j = i + 1; j < n; ++ j) {
67                 Map[i][j] = Map[j][i] =
68                     cal_dis(point[i][0], point[i][1], point[j][0], point[j][1]);
69             }
70         }
71
72         //cout << prim(Map, n) << endl;
73         printf("%.2lf\n", prim(Map, n));
74     }
75     return 0;
76 }

【HDU1162】Eddy's picture(MST基础题),布布扣,bubuko.com

【HDU1162】Eddy's picture(MST基础题)

时间: 2024-10-19 12:18:34

【HDU1162】Eddy's picture(MST基础题)的相关文章

hdu1162 Eddy&#39;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

【HDU1102】Constructing Roads(MST基础题)

最小生成树水题.prim一次AC 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cctype> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 10 #define typec int

hdu 1162 Eddy&#39;s picture 最小生成树入门题 Prim+Kruskal两种算法AC

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

【HDU1301】Jungle Roads(MST基础题)

爽爆.史上个人最快MST的记录7分40s..一次A. 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 10 #define

【HDU3371】Connect the Cities(MST基础题)

注意输入的数据分别是做什么的就好.还有,以下代码用C++交可以过,而且是500+ms,但是用g++就会TLE,很奇怪. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #incl

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

【HDU1875】畅通工程再续(MST基础题)

更改成实形数即可.第一次敲完直接交,CE了一次.晕. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cstdio> 5 #include <cctype> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 10 #define t

【HDU1879】继续畅通工程(MST基础题)

真心大水题...不多说. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cctype> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 10 #define typec int 11 u

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