There is a company with n branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.
The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, they have decided to close down some of these branches (possibly none). However, they want to ensure that the remaining branches have a distance of at most maxDistance from each other.
The distance between two branches is the minimum total traveled length needed to reach one branch from another.
You are given integers n, maxDistance, and a 0-indexed 2D array
roads, where roads[i] = [ui, vi, wi] represents the undirected road between branches ui and vi with length wi.
Return the number of possible sets of closing branches, so that any branch has a distance of at mostmaxDistancefrom any other.
Note that, after closing a branch, the company will no longer have access to any roads connected to it.

Input: n =3, maxDistance =5, roads =[[0,1,2],[1,2,10],[0,2,10]]Output: 5Explanation: The possible sets of closing branches are:- The set [2], after closing, active branches are [0,1] and they are reachable to each other within distance 2.- The set [0,1], after closing, the active branch is[2].- The set [1,2], after closing, the active branch is[0].- The set [0,2], after closing, the active branch is[1].- The set [0,1,2], after closing, there are no active branches.It can be proven, that there are only 5 possible sets of closing branches.

Input: n =3, maxDistance =5, roads =[[0,1,20],[0,1,10],[1,2,2],[0,2,2]]Output: 7Explanation: The possible sets of closing branches are:- The set [], after closing, active branches are [0,1,2] and they are reachable to each other within distance 4.- The set [0], after closing, active branches are [1,2] and they are reachable to each other within distance 2.- The set [1], after closing, active branches are [0,2] and they are reachable to each other within distance 2.- The set [0,1], after closing, the active branch is[2].- The set [1,2], after closing, the active branch is[0].- The set [0,2], after closing, the active branch is[1].- The set [0,1,2], after closing, there are no active branches.It can be proven, that there are only 7 possible sets of closing branches.
Input: n =1, maxDistance =10, roads =[]Output: 2Explanation: The possible sets of closing branches are:- The set [], after closing, the active branch is[0].- The set [0], after closing, there are no active branches.It can be proven, that there are only 2 possible sets of closing branches.
Since n is small (≤10), we can enumerate all possible sets of closing branches using bitmasks. For each set, we compute the shortest paths between remaining branches using Floyd-Warshall, and check if all pairs are within maxDistance.
#include<vector>#include<algorithm>usingnamespace std;
classSolution {
public:int possibleSets(int n, int maxDistance, vector<vector<int>>& roads) {
vector<vector<int>> g(n, vector<int>(n, 1e9));
for (int i =0; i < n; ++i) g[i][i] =0;
for (auto& r : roads) {
g[r[0]][r[1]] = min(g[r[0]][r[1]], r[2]);
g[r[1]][r[0]] = min(g[r[1]][r[0]], r[2]);
}
int ans =0;
for (int mask =0; mask < (1<<n); ++mask) {
vector<vector<int>> dist = g;
for (int k =0; k < n; ++k) if (mask & (1<<k))
for (int i =0; i < n; ++i) for (int j =0; j < n; ++j)
dist[i][k] = dist[k][j] = dist[i][j] =1e9;
for (int k =0; k < n; ++k) if (!(mask & (1<<k)))
for (int i =0; i < n; ++i) if (!(mask & (1<<i)))
for (int j =0; j < n; ++j) if (!(mask & (1<<j)))
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
bool ok = true;
vector<int> active;
for (int i =0; i < n; ++i) if (!(mask & (1<<i))) active.push_back(i);
for (int i : active) for (int j : active) if (i != j)
if (dist[i][j] > maxDistance) ok = false;
if (ok) ++ans;
}
return ans;
}
};
classSolution {
publicintpossibleSets(int n, int maxDistance, int[][] roads) {
int[][] g =newint[n][n];
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) g[i][j]= i==j?0:1000000000;
for (int[] r : roads) {
g[r[0]][r[1]]= Math.min(g[r[0]][r[1]], r[2]);
g[r[1]][r[0]]= Math.min(g[r[1]][r[0]], r[2]);
}
int ans = 0;
for (int mask = 0; mask < (1<<n); ++mask) {
int[][] dist =newint[n][n];
for (int i = 0; i < n; ++i) dist[i]= g[i].clone();
for (int k = 0; k < n; ++k) if ((mask & (1<<k)) != 0)
for (int i = 0; i < n; ++i) dist[i][k]= dist[k][i]= 1000000000;
for (int k = 0; k < n; ++k) if ((mask & (1<<k)) == 0)
for (int i = 0; i < n; ++i) if ((mask & (1<<i)) == 0)
for (int j = 0; j < n; ++j) if ((mask & (1<<j)) == 0)
dist[i][j]= Math.min(dist[i][j], dist[i][k]+dist[k][j]);
boolean ok =true;
for (int i = 0; i < n; ++i) if ((mask & (1<<i)) == 0)
for (int j = 0; j < n; ++j) if (i != j && (mask & (1<<j)) == 0)
if (dist[i][j]> maxDistance) ok =false;
if (ok) ++ans;
}
return ans;
}
}
classSolution {
funpossibleSets(n: Int, maxDistance: Int, roads: Array<IntArray>): Int {
val g = Array(n) { IntArray(n) { if (it==0) 0else1000000000 } }
for (i in0 until n) g[i][i] = 0for (r in roads) {
g[r[0]][r[1]] = minOf(g[r[0]][r[1]], r[2])
g[r[1]][r[0]] = minOf(g[r[1]][r[0]], r[2])
}
var ans = 0for (mask in0 until (1 shl n)) {
val dist = Array(n) { g[it].clone() }
for (k in0 until n) if ((mask and (1 shl k)) !=0)
for (i in0 until n) dist[i][k] = dist[k][i] = 1000000000for (k in0 until n) if ((mask and (1 shl k)) ==0)
for (i in0 until n) if ((mask and (1 shl i)) ==0)
for (j in0 until n) if ((mask and (1 shl j)) ==0)
dist[i][j] = minOf(dist[i][j], dist[i][k]+dist[k][j])
var ok = truefor (i in0 until n) if ((mask and (1 shl i)) ==0)
for (j in0 until n) if (i != j && (mask and (1 shl j)) ==0)
if (dist[i][j] > maxDistance) ok = falseif (ok) ans++ }
return ans
}
}
classSolution:
defpossibleSets(self, n: int, maxDistance: int, roads: list[list[int]]) -> int:
g = [[float('inf')]*n for _ in range(n)]
for i in range(n): g[i][i] =0for u,v,w in roads:
g[u][v] = min(g[u][v], w)
g[v][u] = min(g[v][u], w)
ans =0for mask in range(1<<n):
dist = [row[:] for row in g]
for k in range(n):
if mask & (1<<k):
for i in range(n): dist[i][k] = dist[k][i] = float('inf')
for k in range(n):
ifnot (mask & (1<<k)):
for i in range(n):
ifnot (mask & (1<<i)):
for j in range(n):
ifnot (mask & (1<<j)):
dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j])
ok =True active = [i for i in range(n) ifnot (mask & (1<<i))]
for i in active:
for j in active:
if i != j and dist[i][j] > maxDistance:
ok =Falseif ok: ans +=1return ans
impl Solution {
pubfnpossible_sets(n: i32, max_distance: i32, roads: Vec<Vec<i32>>) -> i32 {
let n = n asusize;
letmut g =vec![vec![1_000_000_000; n]; n];
for i in0..n { g[i][i] =0; }
for r in&roads {
let u = r[0] asusize;
let v = r[1] asusize;
let w = r[2];
g[u][v] = g[u][v].min(w);
g[v][u] = g[v][u].min(w);
}
letmut ans =0;
for mask in0..(1<<n) {
letmut dist = g.clone();
for k in0..n {
if (mask & (1<<k)) !=0 {
for i in0..n { dist[i][k] =1_000_000_000; dist[k][i] =1_000_000_000; }
}
}
for k in0..n {
if (mask & (1<<k)) ==0 {
for i in0..n {
if (mask & (1<<i)) ==0 {
for j in0..n {
if (mask & (1<<j)) ==0 {
let d = dist[i][k] + dist[k][j];
if dist[i][j] > d { dist[i][j] = d; }
}
}
}
}
}
}
letmut ok =true;
let active: Vec<_>= (0..n).filter(|&i| (mask & (1<<i)) ==0).collect();
for&i in&active {
for&j in&active {
if i != j && dist[i][j] > max_distance { ok =false; }
}
}
if ok { ans +=1; }
}
ans
}
}