当前位置: 首页 > article >正文

编程练习7 5G网络建设

需要用到并查集的相关知识:可以参考如下链接

并查集详解(原理+代码实现+应用+优化)-CSDN博客

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;
 
vector<int> split(string params_str) {
    vector<int> p;
    while (params_str.find(" ") != string::npos) {
        int found = params_str.find(" ");
        p.push_back(stoi(params_str.substr(0, found)));
        params_str = params_str.substr(found + 1);
    }    
    p.push_back(stoi(params_str));
    return p;
}
 
vector<string> split_str(string params_str) {
    vector<string> p;
    while (params_str.find(" ") != string::npos) {
        int found = params_str.find(" ");
        p.push_back(params_str.substr(0, found));
        params_str = params_str.substr(found + 1);
    }    
    p.push_back(params_str);
    return p;
}  
 
// 并查集实现
class UnionFind{
    vector<int> root;
    vector<int> rank;
    int cnt;
 
public:
// 初始化数据结构
    UnionFind(int N) : cnt(0){
        root.resize(N+1);
        rank.reserve(N+1);
        for (int i = 0; i < N+1; ++i) {
            root[i] = i;
            rank[i] = 1;
        }
    }
 
    int find(int x) {
        if (x == root[x]) {
            return x;
        }
        return root[x] = find(root[x]);
    }
 
    void union_op(int x, int y) {
        root[find(x)] = find(y);
        cnt+=1;
    }
 
    int get_count(){
        return cnt;
    }
};
 
 
int main()
{
 
    int n,m;
    cin >> n >> m;
    UnionFind uf(n);
 
    vector<vector<int>> networks;
    for (int i = 0; i < m; i++) {
        int a,b,c,d;
        cin >> a >> b >> c >> d;
        if (d == 1) {
            if (uf.find(a) != uf.find(b)) {
                uf.union_op(a, b);
            }
        } else {
            networks.push_back(vector<int>{a,b,c});
        }
    }
    sort(networks.begin(), networks.end(),[](vector<int> a ,vector<int> b){
		return a[2]<b[2];
	});
 
 
    int result = 0;
    int i=0;
    while(true){
        if(i>=networks.size()){
            break;
        } else {
            if (uf.find(networks[i][0]) != uf.find(networks[i][1])) {
                uf.union_op(networks[i][0], networks[i][1]);
                result += networks[i][2];
                if (uf.get_count() == n - 1) {
                    break;
                }
            }
        }
        i+=1;
    }
 
    if(uf.get_count() != n - 1){
        result = -1; 
    }
    cout<<result<<endl;
    return 0;
}


http://www.kler.cn/news/363495.html

相关文章:

  • 管理类联考 信息整理和经验分享
  • proteus中没有STM32F103C8(已解决)
  • yub‘s Algorithmic Adventures_Day12
  • Ubuntu下安装并初始化Git同时添加SSH密钥
  • babylonjs shader学习之copy shadertoy案例
  • 《重置MobaXterm密码并连接Linux虚拟机的完整操作指南》
  • AI手机的启明星:从分级标准到智能体手机
  • 【秋招笔试-支持在线评测】10.23花子秋招(已改编)-三语言题解
  • YOLO11 目标检测 | 导出ONNX模型 | ONNX模型推理
  • C++程序流程结构——选择结构
  • 前端_007_Axios库
  • Flutter SizedBox组件
  • 奇安信勒索解密工具分析及调用
  • Java程序设计:spring boot(9)——应用热部署
  • Java|乐观锁和悲观锁在自旋的时候分别有什么表现?
  • 论文速读:面向单阶段跨域检测的域自适应YOLO(ACML2021)
  • 基于C#开发游戏辅助工具的Windows底层相关方法详解
  • ThreadLocal源码详解
  • 前言——25机械考研复试专业面试问题汇总 机械复试超全流程攻略 机械复试看这一个专栏就够用了!机械复试调剂英语自我介绍口语专业面试常见问题总结 机械保研面试
  • 实用的 Python 小脚本
  • 无线网络的几种认证与加密方式
  • 程序员职业生涯总结之设计自己的人生算法
  • 【github小问题】——push后报错error: src refspec master does not match any
  • 数据链中常见电磁干扰matlab仿真,对比噪声调频,线性调频,噪声,扫频,灵巧五种干扰模型
  • 基于Springboot相亲网站系统的设计与实现
  • C#第三讲:面向对象、类、对象、类成员【定义】