码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-07-25 02:32:14      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   re   c   

很基础的点坐标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基础题)

标签:style   blog   color   os   io   for   re   c   

原文地址:http://www.cnblogs.com/Destiny-Gem/p/3866609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!