蓝桥杯-通电 (最小生成树)

最小生成树的模板题,没有oj,也不知道能得多少分,可能会有一些小错误。

题目:

通电

【问题描述】
2015年,全中国实现了户户通电。作为一名电力建设者,小明正在帮助一带一路上的国家通电。
这一次,小明要帮助 n 个村庄通电,其中 1 号村庄正好可以建立一个发电站,所发的电足够所有村庄使用。
现在,这 n 个村庄之间都没有电线相连,小明主要要做的是架设电线连接这些村庄,使得所有村庄都直接或间接的与发电站相通。
小明测量了所有村庄的位置(坐标)和高度,如果要连接两个村庄,小明需要花费两个村庄之间的坐标距离加上高度差的平方,形式化描述为坐标为 (x_1, y_1) 高度为 h_1 的村庄与坐标为 (x_2, y_2) 高度为 h_2 的村庄之间连接的费用为
sqrt((x_1-x_2)*(x_1-x_2)+(y_1-y_2)*(y_1-y_2))+(h_1-h_2)*(h_1-h_2)。
在上式中 sqrt 表示取括号内的平方根。请注意括号的位置,高度的计算方式与横纵坐标的计算方式不同。
由于经费有限,请帮助小明计算他至少要花费多少费用才能使这 n 个村庄都通电。
【输入格式】
输入的第一行包含一个整数 n ,表示村庄的数量。
接下来 n 行,每个三个整数 x, y, h,分别表示一个村庄的横、纵坐标和高度,其中第一个村庄可以建立发电站。
【输出格式】
输出一行,包含一个实数,四舍五入保留 2 位小数,表示答案。
【样例输入】

4
1 1 3
9 9 7
8 8 6
4 5 4

【样例输出】

17.41

【评测用例规模与约定】
对于 30% 的评测用例,1 <= n <= 10;
对于 60% 的评测用例,1 <= n <= 100;
对于所有评测用例,1 <= n <= 1000,0 <= x, y, h <= 10000。

解法一:prim

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <cmath>
#include <iomanip>
using namespace std;
int n;
double map[1005][1005];
struct MST
{
    double lowestcost;
    int start;
    int x, y, h;
} node[1005];
double prim()
{
    double sum = 0;
    double mincost;
    int mind;
    for (int i = 2; i <= n; i++)
    {
        node[i].lowestcost = map[1][i];
        node[i].start = 1;
    }
    node[1].lowestcost = 0;
    for (int i = 2; i <= n; i++)
    {
        mincost = 0x3f3f3f3f;
        mind = 0;
        for (int j = 2; j <= n; j++)
        {
            if (node[j].lowestcost < mincost && node[j].lowestcost != 0)
            {
                mincost = node[j].lowestcost;
                mind = j;
            }
        }
        sum += node[mind].lowestcost;
        node[mind].lowestcost = 0;
        for (int j = 2; j <= n; j++)
        {
            if (map[mind][j] < node[j].lowestcost)
            {
                node[j].lowestcost = map[mind][j];
                node[j].start = mind;
            }
        }
    }
    return sum;
}
int main()
{
    memset(map, 0x3f, sizeof(map));
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> node[i].x >> node[i].y >> node[i].h;
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            map[i][j] = map[j][i] =
              sqrt((node[i].x - node[j].x) * (node[i].x - node[j].x) + 
                    (node[i].y - node[j].y) * (node[i].y - node[j].y))
                  + (node[i].h - node[j].h) * (node[i].h - node[j].h);
        }
    }
    double cost = prim();

    cout << fixed << setprecision(2) << cost << endl;
    //system("pause");
    return 0;
}

解法二:Kruskal

#include <algorithm>
#include <iostream>
#include <vector>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
const int inf = 0x3f3f3f3f;
int n, m, father[1005];
struct bian
{
    int from, to;
    double cost;
} edge[1005];
struct dian
{
    int x, y, h;
} node[1005];
int find(int x)
{
    int r = x;
    while (r != x)
    {
        r = father[r];
    }
    while (x != r)
    {
        x = father[x];
        father[x] = r;
    }
    return r;
}
void Union(int x, int y)
{
    father[y] = father[father[x]];
}
double Kruskal()
{
    for (int i = 1; i <= n; i++)
    {
        father[i] = i;
    }
    m = n * (n - 1) / 2;
    sort(edge + 1, edge + m + 1, [](const bian &a, const bian &b) { return a.cost < b.cost; });
    int k = 0;
    double sum = 0;
    for (int i = 1; i <= m; i++)
    {
        if (k == n - 1)
            break;
        if (find(edge[i].from) != find(edge[i].to))
        {
            Union(edge[i].from, edge[i].to);
            sum += edge[i].cost;
            k++;
        }
    }
    return sum;
}
int main()
{
    int a, b, c;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> node[i].x >> node[i].y >> node[i].h;
    }
    int k = 0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            edge[k].from = i;
            edge[k].to = j;
            edge[k].cost =
                sqrt((node[i].x - node[j].x) * (node[i].x - node[j].x) + 
                      (node[i].y - node[j].y) * (node[i].y - node[j].y)) 
                + (node[i].h - node[j].h) * (node[i].h - node[j].h);
            k++;
        }
    }
    double cost = Kruskal();
    cout << fixed << setprecision(2) << cost << endl;
    //system("pause");
    return 0;
}

prim和kruskal的区别

总结于:
https://www.cnblogs.com/icode-girl/p/5294886.html
https://zhidao.baidu.com/question/1669669826344255387.html
https://www.jianshu.com/p/dae21874e206 

 Prim算法和Kruskal算法都能从连通图找出最小生成树。

 一、Prim算法:

    首先以一个结点作为最小生成树的初始结点,然后以迭代的方式找出与最小生成树中各结点权重最小边,并加入到最小生成树中。加入之后如果产生回路则跳过这条边,选择下一个结点。当所有结点都加入到最小生成树中之后,就找出了连通图中的最小生成树了。

二、Kruskal算法:

    Kruskal算法与Prim算法的不同之处在于,Kruskal在找最小生成树结点之前,需要对所有权重边做从小到大排序。将排序好的权重边依次加入到最小生成树中,如果加入时产生回路就跳过这条边,加入下一条边。当所有结点都加入到最小生成树中之后,就找出了最小生成树。

       总的来说,prim和kruskal算法其实是互通的,区别在于prim是从点开始找到最小的那棵树,而kruskal是从边开始找最小的那棵树。
       无疑,Kruskal算法在效率上要比Prim算法快,因为Kruskal只需要对权重边做一次排序,而Prim算法则需要做多次排序。
        边数较少可以用Kruskal,因为Kruskal算法每次查找最短的边。 边数较多可以用Prim,因为它是每次加一个顶点,对边数多的适用。
         稠密图——Prim
         稀疏图——Kruskal

 还有一点就是,在用prim做的题中每两个地点是只有一条路的,而kruskal不管是多少条路都适用

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值