博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 1135. Connecting Cities With Minimum Cost
阅读量:4623 次
发布时间:2019-06-09

本文共 2823 字,大约阅读时间需要 9 分钟。

原题链接在这里:

题目:

There are N cities numbered from 1 to N.

You are given connections, where each connections[i] = [city1, city2, cost] represents the cost to connect city1 and city2together.  (A connection is bidirectional: connecting city1 and city2 is the same as connecting city2 and city1.)

Return the minimum cost so that for every pair of cities, there exists a path of connections (possibly of length 1) that connects those two cities together.  The cost is the sum of the connection costs used. If the task is impossible, return -1.

 

Example 1:

Input: N = 3, connections = [[1,2,5],[1,3,6],[2,3,1]]Output: 6Explanation: Choosing any 2 edges will connect all cities so we choose the minimum 2.

Example 2:

Input: N = 4, connections = [[1,2,3],[3,4,4]]Output: -1Explanation: There is no way to connect all cities even if all edges are used.

Note:

  1. 1 <= N <= 10000
  2. 1 <= connections.length <= 10000
  3. 1 <= connections[i][0], connections[i][1] <= N
  4. 0 <= connections[i][2] <= 10^5
  5. connections[i][0] != connections[i][1]

题解:

Try to connect cities with minimum cost, then find small cost edge first, if two cities connected by the edge do no have same ancestor, then union them.

When number of unions equal to 1, all cities are connected. 

Time Complexity: O(mlogm + mlogN). sort takes O(mlogm). find takes O(logN). With path compression and unino by weight, amatorize O(1).

Space: O(N).

AC Java: 

1 class Solution { 2     public int minimumCost(int N, int[][] connections) { 3         Arrays.sort(connections, (a, b) -> a[2]-b[2]); 4          5         int res = 0; 6         UF uf = new UF(N); 7         for(int [] connect : connections){ 8             if(uf.find(connect[0]) != uf.find(connect[1])){ 9                 uf.union(connect[0], connect[1]);10                 res += connect[2];11             }12             13             if(uf.count == 1){14                 return res;15             }16         }17         18         return -1;19     }20 }21 22 class UF{23     int [] parent;24     int [] size;25     int count;26     27     public UF(int n){28         parent = new int[n+1];29         size = new int[n+1];30         for(int i = 0; i<=n; i++){31             parent[i] = i;32             size[i] = 1;33         }34         35         this.count = n;36     }37     38     public int find(int i){39         if(i != parent[i]){40             parent[i] = find(parent[i]);41         }42         43         return parent[i];44     }45     46     public void union(int p, int q){47         int i = find(p);48         int j = find(q);49         if(size[i] > size[j]){50             parent[j] = i;51             size[i] += size[j];52         }else{53             parent[i] = j;54             size[j] += size[i];55         }56         57         this.count--;58     }59 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11280623.html

你可能感兴趣的文章
【DP】 POJ 1191 棋盘分割 记忆化搜索
查看>>
自动化测试 Appium之Python运行环境搭建 Part2
查看>>
说说DBA职责和目标
查看>>
从头认识Spring-2.4 基于java的标准注解装配-@Inject-限定器@Named
查看>>
sql server 实现多表连接查询
查看>>
Python标准库:内置函数getattr(object, name[, default])
查看>>
转:android 自定义RadioButton样式
查看>>
HTTP请求过程
查看>>
织梦多域名解析到同一个空间导致打开链接不一致怎么办?
查看>>
Xcode10 library not found for -lstdc++ 找不到问题
查看>>
Mysql 8.0.13如何重置密码
查看>>
发布功能完成
查看>>
excel 合并单元格
查看>>
iOS设计模式简介
查看>>
c# 扩展方法 奇思妙用 高级篇 九:OrderBy(string propertyName, bool desc)
查看>>
C语言中的地址传递(传指针,传递给形参的指针仍然是实参指针的一份拷贝)
查看>>
redis缓存数据库及Python操作redis
查看>>
opencms忘记Admin用户登录密码解决方案
查看>>
forms组件
查看>>
create-react-app 配置sass
查看>>