Skip to content

2959. Number of Possible Sets of Closing Branches 👍

  • Time: $O(2^n \cdot n^3)$
  • Space: $O(n^2)$
 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
class Solution {
 public:
  int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {
    const int maxMask = 1 << n;
    int ans = 0;

    for (int mask = 0; mask < maxMask; ++mask)
      if (floydWarshall(n, maxDistance, roads, mask) <= maxDistance)
        ++ans;

    return ans;
  }

 private:
  // Returns the maximum distance between any two branches, where the mask
  // represents the selected branches.
  int floydWarshall(int n, int maxDistanceThreshold, vector<vector<int>>& roads,
                    int mask) {
    int maxDistance = 0;
    vector<vector<int>> dist(n, vector<int>(n, maxDistanceThreshold + 1));

    for (int i = 0; i < n; ++i)
      if (mask >> i & 1)
        dist[i][i] = 0;

    for (const vector<int>& road : roads) {
      const int u = road[0];
      const int v = road[1];
      const int w = road[2];
      if (mask >> u & 1 && mask >> v & 1) {
        dist[u][v] = min(dist[u][v], w);
        dist[v][u] = min(dist[v][u], w);
      }
    }

    for (int k = 0; k < n; ++k)
      if (mask >> k & 1)
        for (int i = 0; i < n; ++i)
          if (mask >> i & 1)
            for (int j = 0; j < n; ++j)
              if (mask >> j & 1)
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

    for (int i = 0; i < n; ++i)
      if (mask >> i & 1)
        for (int j = i + 1; j < n; ++j)
          if (mask >> j & 1)
            maxDistance = max(maxDistance, dist[i][j]);

    return maxDistance;
  }
};
 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
class Solution {
  public int numberOfSets(int n, int maxDistance, int[][] roads) {
    final int maxMask = 1 << n;
    int ans = 0;

    for (int mask = 0; mask < maxMask; ++mask)
      if (floydWarshall(n, maxDistance, roads, mask) <= maxDistance)
        ++ans;

    return ans;
  }

  private int floydWarshall(int n, int maxDistanceThreshold, int[][] roads, int mask) {
    int maxDistance = 0;
    int[][] dist = new int[n][n];
    Arrays.stream(dist).forEach(A -> Arrays.fill(A, maxDistanceThreshold + 1));

    for (int i = 0; i < n; ++i)
      if ((mask >> i & 1) == 1)
        dist[i][i] = 0;

    for (int[] road : roads) {
      final int u = road[0];
      final int v = road[1];
      final int w = road[2];
      if ((mask >> u & 1) == 1 && (mask >> v & 1) == 1) {
        dist[u][v] = Math.min(dist[u][v], w);
        dist[v][u] = Math.min(dist[v][u], w);
      }
    }

    for (int k = 0; k < n; ++k)
      if ((mask >> k & 1) == 1)
        for (int i = 0; i < n; ++i)
          if ((mask >> i & 1) == 1)
            for (int j = 0; j < n; ++j)
              if ((mask >> j & 1) == 1)
                dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);

    for (int i = 0; i < n; ++i)
      if ((mask >> i & 1) == 1)
        for (int j = i + 1; j < n; ++j)
          if ((mask >> j & 1) == 1)
            maxDistance = Math.max(maxDistance, dist[i][j]);

    return maxDistance;
  }
}
 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
class Solution:
  def numberOfSets(
      self,
      n: int,
      maxDistance: int,
      roads: list[list[int]],
  ) -> int:
    return sum(self._floydWarshall(n, maxDistance, roads, mask) <= maxDistance
               for mask in range(1 << n))

  def _floydWarshall(
      self,
      n: int,
      maxDistanceThreshold: int,
      roads: list[list[int]],
      mask: int,
  ) -> list[list[int]]:
    """
    Returns the maximum distance between any two branches, where the mask
    represents the selected branches.
    """
    maxDistance = 0
    dist = [[maxDistanceThreshold + 1] * n for _ in range(n)]

    for i in range(n):
      if mask >> i & 1:
        dist[i][i] = 0

    for u, v, w in roads:
      if mask >> u & 1 and mask >> v & 1:
        dist[u][v] = min(dist[u][v], w)
        dist[v][u] = min(dist[v][u], w)

    for k in range(n):
      if mask >> k & 1:
        for i in range(n):
          if mask >> i & 1:
            for j in range(n):
              if mask >> j & 1:
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

    for i in range(n):
      if mask >> i & 1:
        for j in range(i + 1, n):
          if mask >> j & 1:
            maxDistance = max(maxDistance, dist[i][j])

    return maxDistance