Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Bitonic Traveling Salesman Problem (Bitonic TSP)

For given $N$ points in the 2D Euclidean plane, find the distance of the shortest tour that meets the following criteria:

Visit the points according to the following steps:

It starts from the leftmost point (starting point), goes strictly from left to right, and then visits the rightmost point (turn-around point).

Then it starts from the turn-around point, goes strictly from right to left, and then back to the starting point.

Through the processes 1. 2., the tour must visit each point at least once.

Input

The input data is given in the following format:

$N$

$x_1$ $y_1$

$x_2$ $y_2$

...

$x_N$ $y_N$

Constraints

$2 \leq N \leq 1000$

$-1000 \leq x_i, y_i \leq 1000$

$x_i$ differ from each other

The given points are already sorted by x-coordinates

Output

Print the distance of the shortest tour in a line. The output should not have an error greater than 0.0001.

Sample Input 1

3
0 0
1 1
2 0

Sample Output 1

4.82842712

Sample Input 2

4
0 1
1 2
2 0
3 1

Sample Output 2

7.30056308

Sample Input 3

5
0 0
1 2
2 1
3 2
4 0

Sample Output 3

10.94427191

Solution in C (judge verdict: Accepted)

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#include<math.h>
struct Point {
  double x;
  double y;
};
struct Point *p;
double **dist;
double btsp(int size);
int main(void)
{
  int i, n;
  scanf("%d", &n);
  p = (struct Point*)malloc(sizeof(struct Point) * n);
  dist = (double**)malloc(sizeof(double*) * n);
  for (i = 0; i < n; i++) {
    dist[i] = (double*)malloc(sizeof(double) * n);
    scanf("%lf%lf", &p[i].x, &p[i].y);
  }
  printf("%f\n", btsp(n));
  for (i = 0; i < n; i++) free(dist[i]);
  free(dist);
  free(p);
}
double btsp(int size)
{
  int i, j, k;
  double t, ans;
  for (j = 0; j < size; j++) {
    for (i = 0; i < j; i++) {
      if (i == 0 && j == 1) dist[i][j] = hypot(p[i].x - p[j].x, p[i].y - p[j].y);
      else if (i < j - 1) dist[i][j] = dist[i][j-1] +  hypot(p[j-1].x - p[j].x, p[j-1].y - p[j].y);
      else {
        dist[i][j] = INT_MAX;
        for (k = 0; k < i; k++){
          t = dist[k][i] +  hypot(p[k].x - p[j].x, p[k].y - p[j].y);
          if (t < dist[i][j]) dist[i][j] = t;
        }
      }
    }
  }
  ans = INT_MAX;
  for (i = size - 2; i >= 0; i--) {
    t = dist[i][size-1] +  hypot(p[i].x - p[size-1].x, p[i].y - p[size-1].y);
    if (t < ans) ans = t;
  }
  return ans;
}