题解 | #Jungle Roads#

Jungle Roads

https://www.nowcoder.com/practice/75c19b92d6b942f08989b335afbc73c3

// 求最小生成树
// 克鲁斯卡尔算法kruskal:从边集合中,从小到大筛选,若两端点不属于同一集合,加入该边
// 1.若该图已经连通,退出循环输出结果
// 2.或者循环完所有边,输出结果
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Edge
{
    int from;
    int to;
    int length;
};

vector<Edge> edge;//装入所有边
int n;
//并查集算法start
const int maxn=27;
int father[maxn];
int height[maxn];
void init(int n){
    for(int i=0; i<n;i++)
    {
        father[i]=i;
        height[i]=0;
    }
}
int find(int x){
    if(x!=father[x]){
        father[x]=find(father[x]);
    }
    return father[x];
}
void Union(int x,int y){
    x=find(x);
    y=find(y);
    if(x!=y){
        if(height[x]<height[y]){
            father[x]=y;
        }
        else if(height[x]>height[y]){
            father[y]=x;
        }
        else{
            father[y]=x;
            height[x]++;
        }
    }
}
//求连通分量
int countFather(int n){
    int count=0;
    for(int i=0;i<n;i++){
        if(i==find(i))
            count++;
    }
    return count;
}
//并查集算法end

bool comp(Edge x,Edge y){
    return x.length<y.length;
}

void kruskal(int num_of_dot){
    int total=0;
    init(num_of_dot);
    for(auto x:edge){
		//两端点若不都在集合内,加入该边
        if(find(x.from)!=find(x.to)){
            Union(x.from,x.to);
            total+=x.length;
        }
        //如果已经连通,直接输出返回
        if(countFather(num_of_dot)==1){
            cout<<total<<endl;
            return;
        }
    }
    //遍历完所有的边任然不连通
    cout<<total<<endl;
    return;
}

int main(){
    while (cin>>n)
    {
        edge.clear();
        if(n==0) break;
        //输入
        for(int i=0;i<n-1;i++){
            char head,foot;
            int num,number,length;
            cin>>head>>num;
            for(int j=0;j<num;j++){
                cin>>foot>>length;
                Edge e;
                e.from=head-'A';
                e.to=foot-'A';
                e.length=length;
                edge.push_back(e);
            }
        }

        //按边长度从小到大排序
        sort(edge.begin(),edge.end(),comp);

        //克鲁斯卡尔算法
        kruskal(n);
    }
}

全部评论

相关推荐

肥沃富饶:可能初创公司,老板不懂技术
点赞 评论 收藏
分享
01-23 14:54
同济大学 Java
热爱敲代码的程序媛:给你提几点【专业技能】这个模块里面可优化的地方:1.【具备JVM调优经验】可以去b站上搜一下JVM调优的视频,估计一两个小时凭你的学习能力就能掌握JVM调优的实践方面的技能。2.【MySql优化】MySql这一栏,你去b站或者找个博客看看MySql优化,学一下,如果你本身比较熟悉MySql语句的话,那基本半天时间凭你的学习能力MySql语句优化方面的技能你也能掌握个差不多。以上1,2两点主要是因为我看你专业技能大部分都说的是偏理论,没有写应用。再就是最后,你结合你的项目,想一想你的项目中哪些sql语句是可以用MySql优化的,到时候你面试的时候也好结合着说一下。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务