Graph Theory37 sections · 1633 units
Open in Course

Reconstructing the Route

Backtracking from destination

The problem asks for the actual route, not the count. You need to reconstruct the path from city 11 to city nn. While computing dpdp, store parent[v] = u whenever you update dp[v] from dp[u]. After computing dpdp, backtrack from city nn to city 11 using the parent array.

Start at city nn, add it to the path, then move to parent[n], and repeat until you reach city 11. Reverse the path at the end.