Tour
time limit per test 1 second memory limit per test 256 megabytes
John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must determine the shortest closed tour that connects his destinations. Each destination is represented by a point in the plane pi = < xi,yi >. John uses the following strategy: he starts from the leftmost point, then he goes strictly left to right to the rightmost point, and then he goes strictly right back to the starting point. It is known that the points have distinct x-coordinates.
Write a program that, given a set of n points in the plane, computes the shortest closed tour that connects the points according to John’s strategy.

传送门:POJ2677

Sample Input

1
2
3
4
5
6
7
8
9
3
1 1
2 3
3 1
4
1 1
2 3
3 1
4 2

Sample Output

1
2
6.47
7.89

题解

双调欧几里得问题注意若当n<15转换为状压做 双调要先按左端点排序再dp
## AC code:(不包含输入类)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.*;  
import java.util.*;
public class Main {
public static void main(String[] args) {
FastScanner sc=new FastScanner();
PrintWriter pw=new PrintWriter(System.out);
while(sc.hasNext()){
int n=sc.nextInt();
Node[]node=new Node[n+1];
for(int i=1;i<=n;i++){
int x=sc.nextInt();
int y=sc.nextInt();
node[i]=new Node(x,y);
}
Arrays.sort(node,1,n+1);
double[][]dp=new double[n+1][n+1];
dp[1][2]=dp[2][1]=jl(node[1],node[2]);
//dp[j][i] j<i
for(int i=3;i<=n;i++){
for(int j=1;j<=i-2;j++){
dp[j][i]=dp[j][i-1]+jl(node[i],node[i-1]);
}
dp[i-1][i]=Double.MAX_VALUE;
for(int j=1;j<=i-2;j++){
dp[i-1][i]=Math.min(dp[i-1][i],dp[j][i-1]+jl(node[j],node[i]));
}
}
pw.printf("%.2f",dp[n-1][n]+jl(node[n],node[n-1]));
pw.println();
pw.flush();
}

}
static double jl(Node a,Node b){
return Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
}
class Node implements Comparable<Node>{
int x;
int y;
Node(int a,int b){
x=a;
y=b;
}
@Override
public int compareTo(Node o) {
if(this.x<o.x)return -1;
if(this.x>o.x)return 1;
return 0;
}
}