[Swift]LeetCode1168. 水資源分配優化 | Optimize Water Distribution in a Village

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:爲敢(WeiGanTechnologies)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-vfnhdkiu-ke.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

There are n houses in a village. We want to supply water for all the houses by building wells and laying pipes.git

For each house i, we can either build a well inside it directly with cost wells[i], or pipe in water from another well to it. The costs to lay pipes between houses are given by the array pipes, where each pipes[i] = [house1, house2, cost] represents the cost to connect house1 and house2 together using a pipe. Connections are bidirectional.github

Find the minimum total cost to supply water to all houses.數組

Example 1:微信

Input: n = 3, wells = [1,2,2], pipes = [[1,2,1],[2,3,1]]
Output: 3
Explanation: 
The image shows the costs of connecting houses using pipes.
The best strategy is to build a well in the first house with cost 1 and connect the other houses to it with cost 2 so the total cost is 3.

Constraints:ide

  • 1 <= n <= 10000
  • wells.length == n
  • 0 <= wells[i] <= 10^5
  • 1 <= pipes.length <= 10000
  • 1 <= pipes[i][0], pipes[i][1] <= n
  • 0 <= pipes[i][2] <= 10^5
  • pipes[i][0] != pipes[i][1]

村裏面一共有 n 棟房子。咱們但願經過建造水井和鋪設管道來爲全部房子供水。ui

對於每一個房子 i,咱們有兩種可選的供水方案:spa

  • 一種是直接在房子內建造水井,成本爲 wells[i]
  • 另外一種是從另外一口井鋪設管道引水,數組 pipes 給出了在房子間鋪設管道的成本,其中每一個 pipes[i] = [house1, house2, cost] 表明用管道將 house1 和 house2 鏈接在一塊兒的成本。固然,鏈接是雙向的。

請你幫忙計算爲全部房子都供水的最低總成本。code

 

示例:htm

輸入:n = 3, wells = [1,2,2], pipes = [[1,2,1],[2,3,1]]
輸出:3
解釋: 
上圖展現了鋪設管道鏈接房屋的成本。
最好的策略是在第一個房子裏建造水井(成本爲 1),而後將其餘房子鋪設管道連起來(成本爲 2),因此總成本爲 3。

 

提示:

  • 1 <= n <= 10000
  • wells.length == n
  • 0 <= wells[i] <= 10^5
  • 1 <= pipes.length <= 10000
  • 1 <= pipes[i][0], pipes[i][1] <= n
  • 0 <= pipes[i][2] <= 10^5
  • pipes[i][0] != pipes[i][1]

1260 ms

 1 class Solution {
 2     func minCostToSupplyWater(_ n: Int, _ wells: [Int], _ pipes: [[Int]]) -> Int {
 3         var pipes = pipes
 4         var ds:DJSet = DJSet(n + 2)
 5         var m:Int = pipes.count
 6         pipes += [[Int]](repeating:[Int](),count:n)        
 7         for i in 0..<n
 8         {
 9             pipes[m+i] = [n+1, i+1, wells[i]]
10         }
11         pipes.sort(by:{
12             $0[2] < $1[2]
13         })
14         var ans:Int = 0
15         for e in pipes
16         {
17             if !ds.union(e[0], e[1])
18             {
19                 ans += e[2]
20             }
21         }
22         return ans
23     }
24 }
25 
26 public class DJSet
27 {
28     var upper:[Int]
29     
30     init(_ n:Int)
31     {
32         upper = [Int](repeating:-1,count:n)
33     }
34     
35     func root(_ x:Int) -> Int
36     {
37         if(upper[x] < 0)
38         {
39             return x
40         }
41         else
42         {
43             upper[x] = root(upper[x])
44             return upper[x]
45         }
46     }
47     
48     func equiv(_ x:Int,_ y:Int) -> Bool
49     {
50         return root(x) == root(y)
51     }
52     
53     func union(_ x:Int,_ y:Int) -> Bool
54     {
55         var x:Int = root(x)
56         var y:Int = root(y)
57         if x != y
58         {
59             if upper[y] < upper[x]
60             {
61                 var d:Int = x
62                 x = y
63                 y = d
64             }
65             upper[x] += upper[y]
66             upper[y] = x
67         }
68         return x == y
69     }
70     
71     func count() -> Int
72     {
73         var ct:Int = 0
74         for u in upper
75         {
76             if u < 0
77             {
78                 ct += 1
79             }
80         }
81         return ct
82     }
83 }
相關文章
相關標籤/搜索