The shared-cost-after-meeting part is what tripped me up initially.
Model the problem as finding a meeting node M that minimizes dist(A, M) + dist(B, M) + dist(M, D). Precompute shortest distances from A, B, and D using BFS, then iterate over all nodes to find the minimum total cost, returning -1 if no valid meeting node exists.
Pro tip: Clarify that the meeting point must be on a shortest path from the destination to avoid unnecessary detours, and mention that BFS is optimal for unweighted graphs. Also, consider edge cases like A or B already at D, or the graph being disconnected.
Restate the problem: two people start at A and B, can meet at any node M, and then travel together to D. The total cost is dist(A, M) + dist(B, M) + dist(M, D).
Run BFS from A, B, and D to compute the shortest distance from each node to A, B, and D respectively. This takes O(n + m) time per BFS.
For each node M, if all three distances are finite, compute the total cost and keep track of the minimum. If no node yields a finite cost, return -1.
The algorithm runs in O(n + m) time and uses O(n) space for the distance arrays, which is optimal for this problem.
Mention cases where A or B is already at D, or where the graph is disconnected. Also, note that the meeting point must lie on a shortest path from D to avoid extra cost.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.