Question:
Given an undirected graph of N nodes and N-1 edges. The length of all edges are 1. Each edge i has a weight of Wi. (0 <= Wi <= 10.000)
The graph is guaranteed to not have any loops. So there's always one (and only) shortest path between 2 nodes.
Take a pair of node (u, v) from the graph:
- l is the length of shortest the path between the 2 nodes
- w is the edge with the largest weight in the shortest path between the 2 nodes
Given the number K, count the number of pair (u, v) from the graph such that w - l >= K
Example:
N = 3, K = 1
Edges:
1 - 2 - 3
1 - 3 - 2
(Edges are described as: u - v - w)
Answer: 6. All the possible pairs are: (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)
Brute Force Solution (N < 100):
Go over all pairs of (u, v), find the shortest path along side with w and l. Increase the answer if w - l >= K.
Time complexity: O(N^3)
P/S: I have tried and succeeded the brute force solution (with N < 100). But I'm struggling to find a solution with N up to 100.000
u
andv
have different maximum single-edge weight? Is there an assumption that only one shortest path exists from u to v? – גלעד ברקן