题解:本题主要考查深度优先搜索,剪枝。先预处理两点之间的距离,再深搜,再剪枝
代码如下:
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
int n,check[1001];
double x[100],y[20];
double a[1001][1001];
double sum=1231234424.0;
void search(int a1,int a2,double a3)
{
if(a3>sum)return; //剪枝
if(a1==n)
{
sum=min(sum,a3);
return;
}
for(int i=1;i<=n;i++)
if(!check[i])
{
check[i]=1;
search(a1+1,i,a3+a[a2][i]);
check[i]=0;//回溯
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)cin>>x[i]>>y[i];
x[0]=0;y[0]=0;
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
a[i][j]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
search(0,0,0.0);
printf("%.2lf\n",sum);
return 0;
}