代码如下
package Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
public class 吃奶酪 {
static location loc[] = new location[100];
static float sum =0 ;
static int n ;
static ArrayList<Float> arrayList = new ArrayList<>();
static HashSet<Integer> hashSet = new HashSet<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n=scanner.nextInt();
for (int i = 1; i <=n; i++) {
int x = scanner.nextInt();
int y = scanner.nextInt();
location t1 = new location(x, y);
loc[i]=t1;
}
dfs(0,0,n);
float min = Collections.min(arrayList);
System.out.printf("%.2f",min);
hashSet.clear();
}
private static void dfs(int x, int y, int n) {
if (hashSet.size()==n) {
arrayList.add(sum);
return;
}
if (n>0) {
for (int i = 1; i <=n; i++) {
if (!hashSet.contains(i)) {
hashSet.add(i);
int tx = loc[i].x;
int ty = loc[i].y;
float length = (float) Math.sqrt((tx-x)*(tx-x)+(ty-y)*(ty-y)) ;
sum=sum+length;
dfs(tx, ty, n);
hashSet.remove(i);
sum=sum-length;
}
}
}
}
}
class location{
int x;
int y;
public location(int x, int y) {
this.x = x ;
this.y = y ;
}
}