鱼C论坛

 找回密码
 立即注册
查看: 1716|回复: 0

[技术交流] C++刷leetcode(1584. 连接所有点的最小费用)【kruskal算法】

[复制链接]
发表于 2020-10-5 21:17:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目描述:
  1. 给你一个points 数组,表示 2D 平面上的一些点,其中 points[i] = [xi, yi] 。

  2. 连接点 [xi, yi] 和点 [xj, yj] 的费用为它们之间的 曼哈顿距离 :|xi - xj| + |yi - yj| ,其中 |val| 表示 val 的绝对值。

  3. 请你返回将所有点连接的最小总费用。只有任意两点之间 有且仅有 一条简单路径时,才认为所有点都已连接。

  4.  

  5. 示例 1:



  6. 输入:points = [[0,0],[2,2],[3,10],[5,2],[7,0]]
  7. 输出:20
  8. 解释:

  9. 我们可以按照上图所示连接所有点得到最小总费用,总费用为 20 。
  10. 注意到任意两个点之间只有唯一一条路径互相到达。
  11. 示例 2:

  12. 输入:points = [[3,12],[-2,5],[-4,1]]
  13. 输出:18
  14. 示例 3:

  15. 输入:points = [[0,0],[1,1],[1,0],[-1,1]]
  16. 输出:4
  17. 示例 4:

  18. 输入:points = [[-1000000,-1000000],[1000000,1000000]]
  19. 输出:4000000
  20. 示例 5:

  21. 输入:points = [[0,0]]
  22. 输出:0
  23.  

  24. 提示:

  25. 1 <= points.length <= 1000
  26. -106&#160;<= xi, yi <= 106
  27. 所有点&#160;(xi, yi)&#160;两两不同。

  28. 来源:力扣(LeetCode)
  29. 链接:https://leetcode-cn.com/problems/min-cost-to-connect-all-points
  30. 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
复制代码


  1. class Solution {
  2. private:
  3.     vector<int> father;
  4. public:
  5.     struct Edge{
  6.         int a1, a2, distance;
  7.         bool operator < (const Edge & other) const{
  8.             return this -> distance < other.distance;
  9.         }
  10.     };  
  11.     int find_root(int a){
  12.         if(father[a] != a)return find_root(father[a]);
  13.         return a;
  14.     }
  15.     void unite(int a, int b){
  16.         int temp1 = find_root(a);
  17.         int temp2 = find_root(b);
  18.         if(temp1 != temp2){
  19.             father[temp1] = temp2;
  20.         }
  21.         return;
  22.     }

  23.     int solution(int len, vector<Edge>& store){
  24.         //建立父亲集合
  25.         father = vector<int>(len, 0);
  26.         for(int i = 0; i < len; i++){
  27.             father[i] = i;
  28.         }
  29.         int res = 0;
  30.         for(auto cha : store){
  31.             int temp1 = find_root(cha.a1);
  32.             int temp2 = find_root(cha.a2);
  33.             if(temp1 != temp2){
  34.                 res += cha.distance;
  35.                 unite(temp1, temp2);
  36.                 len--;//当所有点都在一棵树的时候停止
  37.                 if(len == 1)return res;
  38.             }
  39.         }
  40.         return res;
  41.     }
  42.     int minCostConnectPoints(vector<vector<int>>& points) {
  43.         int len = points.size();
  44.         vector<Edge> store;
  45.         for(int i = 0; i < len; i++){
  46.             for(int j = i; j < len; j++){
  47.                 store.push_back({i, j,
  48.                 abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1])});
  49.             }
  50.         }
  51.         sort(store.begin(), store.end());
  52.         return solution(len, store);
  53.     }
  54. };
复制代码


参考链接:https://leetcode-cn.com/problems ... ie-da-by-yizhe-shi/
学习视频:https://www.bilibili.com/video/B ... 3053730826471308736

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 11:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表