


‘n = 3’, ‘m = 4’
‘edges = [ [0, 2, 8], [0, 1, 1], [1, 2, 0] ]’

The reachable nodes are highlighted in red color. So, the answer is ‘9’. Node ‘0’, itself included in the answer.
1. For an edge ‘[u, v, sz]’, if ‘sz = 0’, then there is no chain, and ‘(u, v)’ is an edge in the new graph.
2. The graph doesn’t contain parallel edges or self-loops.
The first line of input contains an integer ‘T’ which denotes the number of test cases. Then, the ‘T’ test cases follow.
The first line of each test case contains three integers ‘n’, ‘m’, and ‘numOfPairs’, denoting the number of nodes in the graph, the maximum allowed distance from node ‘0’, and the size of the ‘edges’ array. Then, ‘numOfPairs’ lines follow.
Each line contains three integers representing an element in ‘edges’.
For every test case, print a single line containing a single integer denoting the number of nodes reachable from node ‘0’ in the new graph.
The output of each test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= n <= 500
0 <= m <= 10 ^ 9
0 <= numOfPairs <= min(n * (n - 1) / 2, 10^3)
0 <= edges[i].u < edges[i].v < n
0 <= edges[i].sz <= 10^5
Each element of ‘edges’ contains exactly three integers.
Where ‘T’ is the number of test cases, ‘n’ is the number of nodes, ‘m’ is the maximum allowed distance from node ‘0’, and ‘numOfPairs’ is the size of the array ‘edges’.
Time limit: 1 second
Create the new graph from the original graph by adding the chain nodes. The problem becomes finding the number of nodes whose shortest path from node ‘0’ is less than equal to ‘m’. As the new graph is unweighted, we can use Breadth-first search (BFS) traversal to find the shortest path. The shortest path length will be equivalent to the node’s level in the BFS traversal tree. Perform BFS up to level ‘m’, as we only want the nodes with path length less than equal to ‘m’.
Proof of correctness: As we move level-wise in BFS, if a child node is not visited, it’s not reachable from any previous levels. Its shortest path will be equal to (parent node’s level + 1). Even if it’s reachable from any other node from the next levels, the corresponding path length will always be greater.
Following is an implementation of BFS to solve this problem:
Consider the given graph as a weighted and undirected graph. For any edge ‘(u, v, sz)’ in the distance between ‘u’ and ‘v’ in the new graph is ‘sz + 1’ as there are ‘sz’ nodes in the chain between ‘u’ and ‘v’. So, the edge between ‘(u, v)’ has a weight equal to ‘sz + 1’. Now, we can use Dijkstra’s algorithm to find all reachable nodes in the original graph. However, this will be less than the final answer as we are not counting the partial chain of nodes in the answer.
When we reach a node using the shortest path ‘dist[i]’, we can still move ‘m-dist[i]’ edges from the current node. In the end, for every edge ‘(u, v, sz)’, node ‘u’ can visited ‘l’ (‘= m - dist[u]’) and ‘v’ can visit ‘r’ (‘= m - dist[v]’) chain nodes along the edge ‘(u, v)’. So, the number of chain nodes that we can visit along ‘(u, v)’ is equal to ‘min(sz, l + r)’.
Following is an implementation of Dijkstra’s algorithm using a priority queue (in this case, a min-heap) to solve this problem: