zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    git学习笔记
    angular自定义指令-1
    转 三范式
    CentOS 7 安装NVIDIA驱动实现修改分辨率和扩屏功能
    acm 2034
    acm 2031
    记票统计
    acm 2020 map 逆向输出
    acm 2014
    将输入的字符一个一个读入
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11280623.html
Copyright © 2011-2022 走看看