• 图 最小生成树 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
  • 相关阅读:
    只有开源才能拯救AI
    MyBatis使用注解操作及XML操作
    通过Power Platform自定义D365 CE 业务需求 - 5. 使用Power Virtual Agent
    非对称密钥在ssh远程登陆Linux时的使用
    windows工具:推荐一款可以截长图(滚动截图)的工具FSCapture
    【python】可视化-绘制带有边权重的无向图
    [LeetCode]剑指 Offer 14- I. 剪绳子
    懒人必备爬虫神器—playwright
    maven的nexus私服搭建配置使用
    独立企业签名和共享企业签名的区别
  • 原文地址:https://blog.csdn.net/tina_tian1/article/details/133930043