@marko_ribar wrote:
Here, I revised my code and it works... Slooow, but it works...
HTH., M.R.
Now it's your turn to mark solution, or give a kudo...
Where does it say that the problem is to find the longest path? You can't claim to have solved a problem when you don't know what the problem is.
The solution requires analysis of the elevations of the polylines. The main thalweg is the one whose elevation is lower than all others leading into it. You start at the mouth of the river and follow it upstream. Each time you reach the end of a segment where two tributaries join, you have to analyze the elevations of each of the two upstream paths to determine which is at a lower elevation, and then follow that one to the next junction, and repeat.
Analyzing the elevation involves calculating the lengths of two joining paths to find the elevation at a common distance from where they join, and calculating the stream gradient for the path - you cannot use the elevation of each segment's start point because they are of different lengths. You have to find the elevation at a point on each segment that is equal in distance from the point where the segments join.
The fact that you don't understand the problem is also what leads you to believe that a human can easily solve the problem by 'looking at it'. A human cannot solve the problem by looking at it, because the solution involves the elevations of the polylines (yes, the original sample posted by OP was 'flattened', making it useless for solving the problem).
The solution to the problem is a bit more complicated than most seem to think. It's not finding the longest path. That's not the problem.