• 图 最小生成树 leetcode 1584. 连接所有点的最小费用


    https://leetcode.cn/problems/min-cost-to-connect-all-points/

    Prim

    
    class Solution {
    
    public:
        // prime 
        int minCostConnectPoints(vector<vector<int>>& points) {
            int res = 0;
            int n = points.size();
            using Pair = pair<int,int>; //(-weight, node v);
            vector<vector<Pair>> g(n);
            vector<int> seen(n, 0);
    
            if(n <= 1) return 0;
    
            // 1. construct graph
            for(int i = 0; i < n; i ++){
                for(int j = i + 1; j < n; j ++){
                    int w = abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1]);
                    g[i].push_back({w, j});
                    g[j].emplace_back(w, i);
                }
            }
    
            //2. get minimal edge by priority queue
            priority_queue<Pair> p;
            p.push({0, 0});
            while(n > 0) {
                const int w = -p.top().first;
                const int v = p.top().second;
                p.pop();
    
                if(seen[v]++) continue;
                for(auto& u: g[v]){
                    if(seen[u.second]) continue;
                    p.emplace(-u.first, u.second);
                }
    
                n--;
                res += w; // weight;
            }
            return res;
        }
        
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    Kruskal

    class Solution {
    
    public:
        struct Edge{
            int a, b, w;
            bool operator<(const Edge& e) const {
                return w < e.w;
            }
        };
    
        int minCostConnectPoints(vector<vector<int>>& points) {
            int n = points.size();
            if(n <= 1) return 0;
            //connections每一行是两个顶点和权重
            vector<Edge> connections;
            for(int i = 0; i < n; i ++){
                for(int j = i + 1; j < n; j ++){
                    connections.push_back({i, j, 
                    abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1])});
                }
            }
            return minimumCost(n, connections);
        }
        //Kruskal 
        int minimumCost(int n, vector<Edge>& connections) {
            vector<int> parent = vector<int>(n);
            iota(begin(parent), end(parent),0);
            //把所有边按权值进行排序
            sort(connections.begin(), connections.end());
            function<int(int)> find = [&](int x) {
                return x == parent[x] ? x : parent[x] = find(parent[parent[x]]);
            };
            int res = 0;
            for (Edge& conn : connections) {
                int pa = find(conn.a);
                int pb = find(conn.b);
                if (pa == pb) continue;
                parent[pa] = pb;
                res += conn.w;
                if(--n==1) return res;
            }
            return -1;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
  • 相关阅读:
    腾讯云 BI 数据分析与可视化的快速入门指南
    IO 流(二)
    linux:文件操作(open、write/read、lseek、close)
    AQS理解
    在线记录学习笔记用哪一款工具?
    面试官:单例Bean一定不安全吗?实际工作中如何处理此问题?
    【模电实验】【超值1 + 1】【验证性实验——比例、求和运算电路实验】【验证性实验——各种非正弦信号发生器实验】
    SSL、TLS拒绝服务攻击
    java接口和内部类
    leetcode笔记(自用)
  • 原文地址:https://blog.csdn.net/tina_tian1/article/details/133930043