@vickyvanshaj
In the first solution, min is your final result.
While we calculate distance(newPath) each time, we compare it with min.
If distance(newPath) >= min, then whether we are at point a / b, distance(newPath) will ultimately increase(except at point c which is final point) or remains same, if final point c overlaps with a / b, so there is no meaning of further calculation.
Without the if statement, complexity of problem is O(n * m * k), but I’m not sure of complexity when we include the if statements.
You have found the minimum distance for the paths (x -> n), (n -> m) and (m -> k)
It works well till now. However, after this step you have used the wrong indexes from line 66. Hence the wrong answer.
You are referring to i th index of mx & my and j th index of nx & ny, but it should be the other way around. Make these changes and you will get partially correct answer.
As for the TLE, You are making unnecessary calculations for large test cases. If I have final already less than the minimum distances for the paths (x -> n) or (x -> m), should I still go ahead and calculate the distance for the entire path? Give it a thought
You are committing a common mistake for this problem. Adding up the minimum distances from individual paths will not be the minimum distance for the overall path.
Thanks! I was beginning to suspect as much but still wasn’t sure if it really was that way.
But how do you realize that it’s a mistake? Because at a first glance, it seems logical to take only the min distances. How do you prove that it’s not so?
Edit: Also, the result for the first sample input is 7.57 for my solution which is below the 8.18 given in the sample output. Isn’t that a better answer? Where is the issue?
It doesn’t seem right because there isn’t any connection between the paths you have chosen. The problem wants us to find the path with the overall minimum distance.
If You choose a path A->B and then choose a path C->D, there isnt any connection between B & C here. We need a proper continuous path. This should even answer your second question as to why 7.57 is wrong.
Hmm, yeah I know the obvious reason why it’s giving a smaller answer is because some path is not getting added. But I think I am adding all the paths in that last line
My code uses a similar logic and has the same time complexity (according to my naive brain) but it is still getting TLE in subtask 2. Can you help me make it more efficient and point out where I am going wrong?
@vladprog The approach given is great! Never thought that this can be achieved in a better way. Although I was wandering that if this approach can also be applied when,
The order of visiting the the nodes ‘a’ and ‘b’ is fixed.
Meaning, that starting from (x,y) we can only go to a ∈ A then b ∈ B then c ∈ C.
Can this be achieved in n^2 complexity or we have to simply do a brute force?
Tried using Skip conditions with O(n^3) complexity but the last test case went wrong. Can this sol. be made correct with little changes ? https://www.codechef.com/viewsolution/28510869