-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
65 lines (61 loc) · 1.43 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Linq;
namespace OptimizeWaterDistributionInAVillage
{
class Program
{
static void Main(string[] args)
{
int n = 3;
int[] wells = {1, 2, 2};
int[][] pipes = new[]
{
new[] {1, 2, 1},
new[] {2, 3, 1},
};
Console.WriteLine(MinCostToSupplyWater(n, wells, pipes));
}
private static int[] parent;
private static int[] rank;
public static int MinCostToSupplyWater(int n, int[] wells, int[][] pipes)
{
parent = new int[n + 1];
for (int i = 0; i < parent.Length; i++)
parent[i] = i;
rank = new int[n + 1];
int[][] connections = new int[wells.Length + pipes.Length][];
int index = 0, res = 0;
foreach (var pipe in pipes)
connections[index++] = pipe;
for (int i = 0; i < wells.Length; i++)
connections[index++] = new[] {i + 1, 0, wells[i]};
connections = connections.OrderBy(x => x[2]).ToArray();
foreach (var connection in connections)
{
if (Union(connection[0], connection[1]))
res += connection[2];
}
return res;
}
public static int Find(int x)
{
if (parent[x] != x)
parent[x] = Find(parent[x]);
return parent[x];
}
public static bool Union(int x, int y)
{
int rootX = Find(x), rootY = Find(y);
if (rootX == rootY) return false;
if (rank[rootX] >= rank[rootY])
{
parent[rootY] = rootX;
if (rank[rootX] == rank[rootY])
rank[rootX]++;
}
else
parent[rootX] = rootY;
return true;
}
}
}