hdu-1162 Eddy's picture---浮点数的MST

Posted 努力努力再努力x

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu-1162 Eddy's picture---浮点数的MST相关的知识,希望对你有一定的参考价值。

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1162

题目大意:

给n个点,求MST权值

解题思路:

直接prim算法

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 100 + 10;
 5 const int INF = 1e9 + 7;
 6 double Map[maxn][maxn];
 7 double lowcost[maxn];
 8 int mst[maxn];
 9 int n, m;
10 double prim(int u)
11 {
12     double ans = 0;
13     for(int i = 1; i <= n; i++)
14     {
15         lowcost[i] = Map[u][i];
16         mst[i] = u;
17     }
18     mst[u] = -1;
19     for(int i = 1; i < n; i++)
20     {
21         double minn = INF * 1.0;
22         int v = -1;
23         //寻找lowcost数组里面的未加入mst的最小值
24         for(int j = 1; j <= n; j++)
25         {
26             if(mst[j] != -1 && lowcost[j] < minn)
27             {
28                 v = j;
29                 minn = lowcost[j];
30             }
31         }
32         if(v != -1)
33         {
34             mst[v] = -1;
35             ans += lowcost[v];
36             for(int j = 1; j <= n; j++)
37             {
38                 if(mst[j] != -1 && lowcost[j] > Map[v][j])
39                 {
40                     lowcost[j] = Map[v][j];
41                     mst[j] = v;
42                 }
43             }
44         }
45     }
46     printf("%.2f\n", ans);
47 }
48 struct node
49 {
50     double x, y;
51 }a[maxn];
52 double dis(node a, node b)
53 {
54     return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
55 }
56 int main()
57 {
58     while(scanf("%d", &n) != EOF)
59     {
60         for(int i = 1; i <= n; i++)
61             for(int j = 1; j <= n; j++)Map[i][j] = INF * 1.0;
62         memset(lowcost, 0, sizeof(lowcost));
63         for(int i = 1; i <= n; i++)
64         {
65             scanf("%lf%lf", &a[i].x, &a[i].y);
66         }
67         for(int i = 1; i <= n; i++)
68         {
69             for(int j = i + 1; j <= n; j++)
70             {
71                 double d = dis(a[i], a[j]);
72                 Map[i][j] = Map[j][i] = d;
73             }
74         }
75         prim(1);
76 
77     }
78     return 0;
79 }

 

以上是关于hdu-1162 Eddy's picture---浮点数的MST的主要内容,如果未能解决你的问题,请参考以下文章

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

HDU 1162 Eddy's picture (最小生成树 prim)

HDU 1162 Eddy's picture (最小生成树)(java版)

hdu-1162 Eddy's picture---浮点数的MST

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

hdu 1162 Eddy&#39;s picture (Kruskal算法,prim算法,最小生成树)