1135. Connecting Cities With Minimum Cost

1135. Connecting Cities With Minimum Cost

Description

Difficulty: Medium

Related Topics: Union Find, Graph, Heap (Priority Queue), Minimum Spanning Tree

There are n cities labeled from 1 to n. You are given the integer n and an array connections where connections[i] = [xi, yi, costi] indicates that the cost of connecting city xi and city yi (bidirectional connection) is costi.

Return the minimum cost to connect all the n cities such that there is at least one path between each pair of cities. If it is impossible to connect all the n cities, return -1,

The cost is the sum of the connections’ costs used.

Example 1:

1
2
3
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:

1
2
3
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.

Constraints:

  • 1 <= n <= 104
  • 1 <= connections.length <= 104
  • connections[i].length == 3
  • 1 <= xi, yi <= n
  • xi != yi
  • 0 <= costi <= 105

Hints/Notes

  • Kruskal’s algorithm: union find + weight
  • Prim’s algorithm: BFS + priority queue

Solution

Language: C++

Kruskal’s algorithm

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
class Solution {
public:
    vector<int> parent;
    int count;

    int minimumCost(int n, vector<vector<int>>& connections) {
        int count = n;
        for (int i = 0; i <= n; i++) {
            parent.push_back(i);
        }

        auto cmp = [](vector<int> lhs, vector<int> rhs) {
            return lhs[2] < rhs[2];
        };
        sort(connections.begin(), connections.end(), cmp);

        int sum = 0;
        for (auto connection : connections) {
            int p = connection[0];
            int q = connection[1];
            int findP = find(p);
            int findQ = find(q);
            if (findP == findQ) {
                continue;
            } else {
                parent[findP] = findQ;
                sum += connection[2];
                count--;
            }
        }

        return count == 1 ? sum : -1;
    }

    int find(int node) {
        if (parent[node] != node) {
            parent[node] = find(parent[node]);
        }
        return parent[node];
    }
};

Prim’s algorithm

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
class Solution {
public:
vector<vector<vector<int>>> graph;
vector<bool> visited;
priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;

int minimumCost(int n, vector<vector<int>>& connections) {
int count = n;
graph = vector<vector<vector<int>>>(n + 1, vector<vector<int>>());
visited = vector<bool>(n + 1, false);

for (auto connection : connections) {
int p = connection[0];
int q = connection[1];
int w = connection[2];
graph[p].push_back({w, q});
graph[q].push_back({w, p});
}

int cost = 0;
visited[1] = true;
cut(1);
while (!pq.empty()) {
vector<int> pair = pq.top();
pq.pop();
int node = pair[1];
if (visited[node]) {
continue;
}
n--;
int w = pair[0];
visited[node] = true;
cost += w;
cut(node);
}

return n == 1 ? cost : -1;
}

void cut(int node) {
for (auto edge : graph[node]) {
int to = edge[1];
if (visited[to]) {
continue;
} else {
int w = edge[0];
pq.push({w, to});
}
}
}
};