This approach involves using the Union-Find data structure (also known as Disjoint Set Union, DSU) to manage connections between cities efficiently. By iterating over all roads, we determine which cities are interconnected. The key is to keep track of the minimum weight of a road that connects these cities after they are all unified.
This solution utilizes an efficient means of finding and unifying elements, reducing the overhead of nested loops and allowing operations near constant-time with path compression and union by rank techniques.
Time Complexity: O(E log* V)
Space Complexity: O(V)
1#include <stdio.h>
2#include <stdlib.h>
3#include <limits.h>
4
5int find(int* parent, int i) {
6 if (parent[i] != i)
7 parent[i] = find(parent, parent[i]);
8 return parent[i];
9}
10
11void union_set(int* parent, int* rank, int x, int y) {
12 int rootX = find(parent, x);
13 int rootY = find(parent, y);
14 if (rootX != rootY) {
15 if (rank[rootX] > rank[rootY]) {
16 parent[rootY] = rootX;
17 } else if (rank[rootX] < rank[rootY]) {
18 parent[rootX] = rootY;
19 } else {
20 parent[rootY] = rootX;
21 rank[rootX]++;
22 }
23 }
24}
25
26int minScore(int n, int** roads, int roadsSize, int* roadsColSize) {
27 int* parent = (int*)malloc((n + 1) * sizeof(int));
28 int* rank = (int*)calloc(n + 1, sizeof(int));
29 for (int i = 1; i <= n; i++)
30 parent[i] = i;
31
32 int minDistance = INT_MAX;
33 for (int i = 0; i < roadsSize; i++) {
34 int a = roads[i][0];
35 int b = roads[i][1];
36 int dist = roads[i][2];
37 union_set(parent, rank, a, b);
38 }
39
40 int root1 = find(parent, 1);
41 int rootN = find(parent, n);
42
43 for (int i = 0; i < roadsSize; i++) {
44 int a = roads[i][0];
45 int b = roads[i][1];
46 int dist = roads[i][2];
47 if (find(parent, a) == root1 || find(parent, b) == root1)
48 if (dist < minDistance)
49 minDistance = dist;
50 }
51
52 free(parent);
53 free(rank);
54 return minDistance;
55}
56
This solution uses a Disjoint Set Union (DSU) to identify all the cities that are connected together. It then iterates over the connections (roads), linking them up, and finally examines the edges to find the smallest possible score afterward.
Another intuitive approach is to use graph traversal techniques such as BFS or DFS. Starting from city 1, you can explore all reachable cities while dynamically updating the minimum edge encountered during the exploration. This ensures you calculate the smallest score path by evaluating all potential paths to the destination city.
Time Complexity: O(V + E)
Space Complexity: O(V + E)
1def minScore(n, roads):
2 from collections import defaultdict
3 graph = defaultdict(list)
4
5 for a, b, dist in roads:
6 graph[a].append((b, dist))
7 graph[b].append((a, dist))
8
9 def dfs(node, visited, currentMin):
10 visited[node] = True
11 for adj, dist in graph[node]:
12 if not visited[adj]:
13 currentMin = min(currentMin, dist)
14 currentMin = dfs(adj, visited, currentMin)
15 return currentMin
16
17 visited = [False] * (n + 1)
18 return dfs(1, visited, float('inf'))
19
Python implementation captures the graph using a dictionary of lists. Using DFS starting from node 1, it identifies and updates the lowest edge score to connect to city n.