数据结构与算法-图论(二)-最小生成树(MST)

数据结构与算法-图论(二)-最小生成树(MST)

​ 在一个无向连通图中,如果存在一个连通子图包含原图中所有的结点和部分边,且这个子图不存在回路,那么我们称这个子图为原图的一棵生成树。在带权图中,所有的生成树中边权的和最小的那棵(或几棵)被称为最小生成树。

我们先来看这样一个定理:

​ 在要求解的连通图中,任意选择一些点属于集合A,剩余的点属于集合B,必定存在一棵最小生成树包含两个顶点分别属于集合A和集合B的边(即连通两个集合的边)中权值最小的边。

​ 这个结论就是我们将要介绍的求最小生成树Kruskal算法的算法原理,它按照如下步骤求解最小生成树:

  1. 初始时所有结点属于孤立的集合。
  2. 按照边权递增顺序遍历所有的边,若遍历到的边两个顶点仍分属不同的集合(该边即为连通这两个集合的边中权值最小的那条)则确定该边为最小生成树上的一条边,并将两个顶点分属的集合合并。
  3. 遍历完所有边后,原图上所有结点属于同一个集合则被选取的边和原图中所有结点构成最小生成树;否则原图不连通,最小生成树不存在。
题目描述:

某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要间接通过公路可达即可),并要求铺设的公路总长度为最短。请计算最小的公路总长度。

输入:

测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N(< 100):随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。当N为0时,输入结束,该用例不被处理。

输出:

对每个测试用例,在1行里输出最短的公路总长度。

样例输入:
3
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0
样例输出:
3
5
代码如下:
#include<iostream>
#include<algorithm>

using namespace std;

#define N 101
int pre[N];
int find(int x){
    int r = x;
    while(r != pre[r]){
        r = pre[r];
    }
    int i = x,j;
    while(i != r){
        j = pre[i];
        pre[i] = r;
        i = j;
    }
    return r;
}

struct Edge{	// 边结构体
    int a,b;	// 边两个顶点的编号
    int cost;	// 边的权值
    bool operator<(const Edge &A)const{
        return cost < A.cost;
    }
}edge[6000];

int main(){
    int n;
    while(scanf("%d",&n)!=EOF && n){
        for(int i = 1 ; i <= n ; i++)
            pre[i] = i;
        int k = n*(n-1)/2;
        for(int i = 0 ; i < k ; i++){
            scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].cost);
        }
        sort(edge,edge+k);
        int ans = 0;
        for(int i = 0 ; i < k ; i++){
            int a = find(edge[i].a);
            int b = find(edge[i].b);
            if(a!=b){
                pre[a] = b;
                ans += edge[i].cost;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
运行结果:

在看了如此明显的最小生成树后,我们再来看略微含蓄一点的例题。

题目描述:

In an episode of the Dick Van Dyke show,little Richie connect the freckles on his Dad’s back to form a picture of he Liberty Bell.Alas, one of the freckles turns out to be a scar,so his Ripley’s engagement falls through.Consider Dick’s back to be a plane with freckles at various(x,y) locations.Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs,possibly lifting the pen between lines.When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.

输入:

The first line contains 0 < n <= 100,the number of freckles on Dick’s back.For each freckle,a line follows;each following line contains two real numbers indicating the (x,y) coordinates of the freckle.

输出:

Your program prints a single real number to two decimal places:the minimum total length of ink lines that can connect all the freckles.

样例输入:
3
1.0 1.0
2.0 2.0
2.0 4.0
样例输出:
3.41

代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>

using namespace std;

#define N 101
int pre[N];

int find(int x){
    int r = x;
    while(r != pre[r]){
        r = pre[r];
    }
    int i = x,j;
    while(i != r){
        j = pre[i];
        pre[i] = r;
        i = j;
    }
    return r;
}

struct Edge{
    int a,b;	
    double cost;	
    bool operator<(const Edge &A)const{	
        return cost < A.cost;
    }
}edge[6000];

struct Point{// 点结构体
    double x,y;// 点的两个坐标值
    double getDistance(Point A){// 计算点之间的距离
        double tmp = (x-A.x)*(x-A.x)+(y-A.y)*(y-A.y);
        return sqrt(tmp);
    }
}list[101];

int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i = 1 ; i <= n ; i++){
            scanf("%lf%lf",&list[i].x,&list[i].y);
        }// 输入
        int size = 0;	// 抽象出的边的总数
        for(int i = 1 ; i <= n ; i++){
            for(int j = i+1 ; j <= n ; j++){
                edge[size].a = i;
                edge[size].b = j;	// 该边的两个顶点编号
                edge[size].cost = list[i].getDistance(list[j]);
                size++;	// 边的总数增加
            }	// 遍历所有的点对
        }
        sort(edge,edge+size);
        for(int i = 1 ; i <= n ; i++)
            pre[i] = i;
        double ans = 0;
        for(int i = 0 ; i < size ; i++){
            int a = find(edge[i].a);
            int b = find(edge[i].b);
            if(a != b){
                pre[a] = b;
                ans += edge[i].cost;
            }
        }// 最小生成树
        printf("%.2lf\n",ans);	// 输出
    }
    return 0;
}
运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值