原题链接
正常的dfs最后一个点t了, 只好用卡时玄学,就ac了。
正解应该是状压dp。
import java.util.*;
public class Main {
static int N = 16, n;
static double res = 1e9;
static double x[] = new double[N], y[] = new double[N];
static boolean st[] = new boolean[N];
static int l = 0;
static void dfs(int u, double len, int sum) {
l ++ ;
if(l > 50000000) { // 卡时玄学大法
System.out.printf("%.2f", res);
System.exit(0);
}
if(len >= res)
return;
if(sum == n) {
res = len;
return;
}
st[u] = true;
for(int i = 1; i <= n; i ++ ) {
if(!st[i]) {
dfs(i, len + get(u, i), sum + 1);
}
}
st[u] = false;
}
static double get(int a, int b) {
return Math.sqrt((x[a] - x[b]) * (x[a] - x[b]) + (y[a] - y[b]) * (y[a] - y[b]));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(int i = 1; i <= n; i ++ ) {
x[i] = sc.nextDouble();
y[i] = sc.nextDouble();
}
dfs(0, 0.0, 0);
System.out.printf("%.2f", res);
}
}