I am trying to solve an algorithmic problem.

First of all the user inputs the coordinates of some nodes.

I have to go to the node with the biggest x( let name it E) and then to return to the start that will always be the node (0,0)( let name it S).

In this cycle i have to pass from all the nodes but there are some restrictions. When i go to the E i should go along x axes and xprevious<xnext ( i should go only toward nodes with bigger x than x of my current node). When I return to S is the same process but xprevious>xnext.

I have to find the shortest path to do that cycle.

Somewhere i found that i have to use dynamic programming but i am not so good in that and i didn't found many information for that solution.

Can you help me to figure out how to solve that?

Thanks in advance.