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 }
  • 相关阅读:
    如何使用SAP Intelligent Robotic Process Automation自动操作Excel
    OpenSAML 使用引导 IV: 安全特性
    Spring Cloud Zuul 网关使用与 OAuth2.0 认证授权服务
    微服务架构集大成者—Spring Cloud (转载)
    Spring Cloud Eureka 服务注册列表显示 IP 配置问题
    使用 Notification API 开启浏览器桌面提醒
    SignalR 中使用 MessagePack 序列化提高 WebSocket 通信性能
    配置 Nginx 的目录浏览功能
    关于 Nginx 配置 WebSocket 400 问题
    Migrate from ASP.NET Core 2.0 to 2.1
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11280623.html
Copyright © 2011-2022 走看看