AC代码
#include <bits/stdc++.h>
using namespace std;
#define ms(a,b) memset(a,b,sizeof(a))
typedef long long ll;
struct Point{
double x,y;
}point[20];
int n;
double dist[20][20];
double ans;
int vis[20];
inline int read()
{
int X=0,w=0; char ch=0;
while(!isdigit(ch))
{
w|=ch=='-';
ch=getchar();
}
while(isdigit(ch))
{
X=(X<<3)+(X<<1)+(ch^48);
ch=getchar();
}
return w?-X:X;
}
double dist_calc(double x1,double y1,double x2,double y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void dfs(double x,double y,int num,double distnow,int now)
{
if (distnow>ans) return;
if (num==n)
{
ans=min(ans,distnow);
return;
}
for (int i=1;i<=n;i++)
{
if ( vis[i] )
{
vis[i] = 0;
dfs(point[i].x,point[i].y,num+1,distnow+dist[now][i],i);
vis[i] = 1;
}
}
}
int main()
{
n=read();
point[0].x=0,point[0].y=0;
for (int i=1;i<=n;i++)
{
cin>>point[i].x>>point[i].y;
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
dist[i][j]=dist_calc(point[i].x,point[i].y,point[j].x,point[j].y);
}
}
for (int i=1;i<=n;i++) dist[0][i]=dist[i][0]=dist_calc(0,0,point[i].x,point[i].y);
vis[0]=0;
ms( vis, 1);
ans=1<<30; dfs(0,0,0,0,0);
printf("%0.2lf
",ans);
return 0;
}