To solve this problem, we can interpret it as a graph problem, where rooms are nodes and doors with locks are edges. We need to ensure that all rooms can be accessed, which means the graph must be connected, and we want to minimize the cost of the keys (which represent the edges). This is a classic Minimum Spanning Tree (MST) problem.
To solve the MST problem, we can use Kruskal's algorithm or Prim's algorithm. In this explanation, I'll outline the solution using Kruskal's algorithm.
Kruskal's Algorithm
-
Initialize:
- Start by sorting all the edges (doors) based on their weights (cost of keys).
- Initialize a union-find data structure to keep track of the connected components.
-
Process Edges:
- Iterate over the sorted edges.
- For each edge, check if the nodes (rooms) it connects are in different components.
- If they are, add this edge to the MST and unite the components.
- If they are in the same component, skip this edge to avoid cycles.
-
Terminate:
- Stop when you have added ( n-1 ) edges to the MST, where ( n ) is the number of nodes (rooms).
- The sum of the weights of these edges is the minimum cost to connect all rooms.
Implementation
Let's outline the implementation in Java:
import java.util.*;
public class MinimumCostToConnectRooms {
static class Edge implements Comparable<Edge> {
int from, to, weight;
Edge(int from, int to, int weight) {
this.from = from;
this.to = to;
this.weight = weight;
}
@Override
public int compareTo(Edge other) {
return Integer.compare(this.weight, other.weight);
}
}
static class UnionFind {
private int[] parent, rank;
UnionFind(int n) {
parent = new int[n];
rank = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 1;
}
}
int find(int u) {
if (parent[u] != u) {
parent[u] = find(parent[u]);
}
return parent[u];
}
boolean union(int u, int v) {
int ru = find(u);
int rv = find(v);
if (ru == rv) return false;
if (rank[ru] < rank[rv]) {
parent[ru] = rv;
} else if (rank[ru] > rank[rv]) {
parent[rv] = ru;
} else {
parent[rv] = ru;
rank[ru]++;
}
return true;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
List<Edge> edges = new ArrayList<>();
for (int i = 0; i < m; i++) {
int weight = scanner.nextInt();
int from = scanner.nextInt() - 1; // convert to 0-based index
int to = scanner.nextInt() - 1; // convert to 0-based index
edges.add(new Edge(from, to, weight));
}
Collections.sort(edges);
UnionFind uf = new UnionFind(n);
int mstCost = 0;
int edgesUsed = 0;
for (Edge edge : edges) {
if (uf.union(edge.from, edge.to)) {
mstCost += edge.weight;
edgesUsed++;
if (edgesUsed == n - 1) break; // MST completed
}
}
System.out.println(mstCost);
}
}
Explanation
-
Edge and UnionFind Classes:
Edge class represents a door between two rooms with a cost.
UnionFind class helps in efficiently managing connected components and detecting cycles.
-
Input Handling:
- We read the number of rooms (( n )) and the number of doors (( m )).
- We read each door's cost and the rooms it connects, adjusting for zero-based indexing.
-
Kruskal’s Algorithm:
- Sort all edges by weight and try to add them to the MST using the union-find structure.
- If an edge connects different components, it's added to the MST, and components are united.
-
Output:
- Print the total cost of the MST once all rooms are connected.
This implementation efficiently finds the minimum cost to connect all rooms in Aunt Riza's house.