我朋友的写法,AC了,留在ide里,就是排列组合吧。
public class lovely { static double[][] arr=new double[6][2]; static double [] []dis=new double[6][6]; public static void main(String[] args) { double distance=0; Scanner sc = new Scanner(System.in); for(int i=1;i<=5;i++) { for(int j=0;j<2;j++)arr[i][j]=sc.nextInt(); } for(int i=0;i<6;i++) { for(int j=0;j<6;j++) { dis[i][j]=Math.sqrt(Math.abs(arr[i][0]-arr[j][0])*Math.abs(arr[i][0]-arr[j][0])+Math.abs(arr[i][1]-arr[j][1])*Math.abs(arr[i][1]-arr[j][1])); } } ArrayList<Integer> save=new <Integer>ArrayList(); save.add(1); save.add(2); save.add(3); save.add(4); save.add(5); distance=showin(0,save); System.out.println((int)distance); } public static double showin(Integer b,ArrayList<Integer> save) { Iterator<Integer> it = save.iterator(); double max=80000000; if(!it.hasNext())return 0; if(save.size()==1) { Integer i=it.next(); return dis[0][i]+dis[b][i]; } while (it.hasNext()) { Integer a=it.next(); ArrayList<Integer> newsave=new <Integer>ArrayList(); newsave.addAll(save); newsave.remove(a); Double c=showin(a, newsave); if(max>dis[b][a]+c)max=dis[b][a]+c; } return max; } }