编程练习7 5G网络建设

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

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

cpp 复制代码
#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;
}
相关推荐
InHand云飞小白10 小时前
告别有线宽带束缚!5G Wi-Fi 6 CPE赋能企业网络新范式
网络·5g·网络运维·5g路由器·连锁门店·5gcpe·商业联网
Wireless_wifi610 小时前
Why IPQ5018 Continues to Thrive in the Wi-Fi 7 Era
linux·5g
Multipath71221 小时前
无人区不掉线:多链路聚合路由,为环塔拉力赛筑起“空中通讯走廊”
网络·5g·安全·无人机·实时音视频
WHS-_-20221 天前
Radar Signal Reconstruction in Severe Interference via Robust Tensor Completion
5g
写代码写到手抽筋3 天前
5G上行DCI字段判定:端口 流数 PMI选择详解
java·算法·5g
ZK_H5 天前
MFC学习——简易计算器以及跨应用通信
学习·5g·mfc
星恒讯工业路由器5 天前
星恒讯便携移动路由器的好处
网络·5g·智能路由器·信息与通信·wifi6·便携
写代码写到手抽筋5 天前
天线极化方向核心原理及工程
5g
InHand云飞小白5 天前
连锁门店IT运维实战:如何用“云+端“架构解决分布式网络管理难题
运维·网络·5g·安全·智能路由器·5g路由器
GTfiber7 天前
光特通信高速率SFP光模块:5G通信高质量发展
5g