PREREQUISITE : DFS Explanation : Simple DFS Problem just we will add a distance array and pass a extra parameter to the function.