LeetCode 1135. Connecting Cities With Minimum Cost

原题链接在这里:https://leetcode.com/problems/connecting-cities-with-minimum-cost/

题目:

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: 6
Explanation: 
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: -1
Explanation: 
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值